<s> import base64 <EOL> import json <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import yaml <EOL> from appscale_logger import AppScaleLogger <EOL> from custom_exceptions import AppScaleException <EOL> from custom_exceptions import AppScalefileException <EOL> from custom_exceptions import BadConfigurationException <EOL> from custom_exceptions import ShellException <EOL> from appscale_tools import AppScaleTools <EOL> from local_state import LocalState <EOL> from parse_args import ParseArgs <EOL> from remote_helper import RemoteHelper <EOL> from registration_helper import RegistrationHelper <EOL> class AppScale ( ) : <EOL> """<STR_LIT>""" <EOL> APPSCALEFILE = "<STR_LIT>" <EOL> TEMPLATE_CLOUD_APPSCALEFILE = path = os . path . dirname ( __file__ ) + os . sep + "<STR_LIT>" <EOL> TEMPLATE_CLUSTER_APPSCALEFILE = path = os . path . dirname ( __file__ ) + os . sep + "<STR_LIT>" <EOL> APPSCALE_DIRECTORY = os . path . expanduser ( "<STR_LIT>" ) + os . sep + "<STR_LIT>" + os . sep <EOL> TERMINATE = "<STR_LIT>" <EOL> USAGE = """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> pass <EOL> def get_appscalefile_location ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . getcwd ( ) + os . sep + self . APPSCALEFILE <EOL> def read_appscalefile ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( self . get_appscalefile_location ( ) ) as file_handle : <EOL> return file_handle . read ( ) <EOL> except IOError : <EOL> raise AppScalefileException ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> def get_locations_json_file ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> appscale_dir = os . path . expanduser ( "<STR_LIT>" ) + os . sep + "<STR_LIT>" <EOL> json_file = appscale_dir + os . sep + "<STR_LIT>" + keyname + "<STR_LIT>" <EOL> return json_file <EOL> def get_nodes ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> with open ( self . get_locations_json_file ( keyname ) ) as locations_file : <EOL> return json . loads ( locations_file . read ( ) ) <EOL> except IOError : <EOL> raise AppScaleException ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def get_head_node ( self , nodes ) : <EOL> """<STR_LIT>""" <EOL> for node in nodes : <EOL> if '<STR_LIT>' in node [ '<STR_LIT>' ] : <EOL> return node [ '<STR_LIT>' ] <EOL> raise AppScaleException ( '<STR_LIT>' ) <EOL> def get_key_location ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> appscale_dir = os . path . expanduser ( "<STR_LIT>" ) + os . sep + "<STR_LIT>" <EOL> key_file = appscale_dir + os . sep + keyname + "<STR_LIT>" <EOL> return key_file <EOL> def init ( self , environment ) : <EOL> """<STR_LIT>""" <EOL> appscalefile_location = self . get_appscalefile_location ( ) <EOL> if os . path . exists ( appscalefile_location ) : <EOL> raise AppScalefileException ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> if environment == '<STR_LIT>' : <EOL> template_file = self . TEMPLATE_CLOUD_APPSCALEFILE <EOL> elif environment == '<STR_LIT>' : <EOL> template_file = self . TEMPLATE_CLUSTER_APPSCALEFILE <EOL> else : <EOL> raise BadConfigurationException ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> shutil . copy ( template_file , appscalefile_location ) <EOL> def up ( self ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> if not LocalState . ensure_appscalefile_is_up_to_date ( ) : <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> if "<STR_LIT>" in contents_as_yaml : <EOL> ips_layout = base64 . b64encode ( yaml . dump ( contents_as_yaml [ "<STR_LIT>" ] ) ) <EOL> if "<STR_LIT>" in contents_as_yaml : <EOL> disks = base64 . b64encode ( yaml . dump ( contents_as_yaml [ "<STR_LIT>" ] ) ) <EOL> if "<STR_LIT>" in contents_as_yaml : <EOL> user_commands = base64 . b64encode ( yaml . dump ( <EOL> contents_as_yaml [ "<STR_LIT>" ] ) ) <EOL> if not "<STR_LIT>" in contents_as_yaml : <EOL> if not self . valid_ssh_key ( contents_as_yaml ) : <EOL> add_keypair_command = [ ] <EOL> if "<STR_LIT>" in contents_as_yaml : <EOL> add_keypair_command . append ( "<STR_LIT>" ) <EOL> add_keypair_command . append ( str ( contents_as_yaml [ "<STR_LIT>" ] ) ) <EOL> add_keypair_command . append ( "<STR_LIT>" ) <EOL> add_keypair_command . append ( ips_layout ) <EOL> options = ParseArgs ( add_keypair_command , "<STR_LIT>" ) . args <EOL> AppScaleTools . add_keypair ( options ) <EOL> command = [ ] <EOL> for key , value in contents_as_yaml . items ( ) : <EOL> if key in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> os . environ [ key ] = value <EOL> continue <EOL> if value is True : <EOL> command . append ( str ( "<STR_LIT>" % key ) ) <EOL> elif value is False : <EOL> pass <EOL> else : <EOL> if key == "<STR_LIT>" : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( ips_layout ) <EOL> elif key == "<STR_LIT>" : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( disks ) <EOL> elif key == "<STR_LIT>" : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( user_commands ) <EOL> else : <EOL> command . append ( str ( "<STR_LIT>" % key ) ) <EOL> command . append ( str ( "<STR_LIT:%s>" % value ) ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> AppScaleTools . run_instances ( options ) <EOL> def valid_ssh_key ( self , config ) : <EOL> """<STR_LIT>""" <EOL> keyname = config [ "<STR_LIT>" ] <EOL> verbose = config . get ( '<STR_LIT>' , False ) <EOL> if not isinstance ( config [ "<STR_LIT>" ] , dict ) : <EOL> raise BadConfigurationException ( "<STR_LIT>" "<STR_LIT>" ) <EOL> ssh_key_location = self . APPSCALE_DIRECTORY + keyname + "<STR_LIT>" <EOL> if not os . path . exists ( ssh_key_location ) : <EOL> return False <EOL> all_ips = self . get_all_ips ( config [ "<STR_LIT>" ] ) <EOL> for ip in all_ips : <EOL> if not self . can_ssh_to_ip ( ip , keyname , verbose ) : <EOL> return False <EOL> return True <EOL> def get_all_ips ( self , ips_layout ) : <EOL> """<STR_LIT>""" <EOL> all_ips = [ ] <EOL> for _ , ip_or_ips in ips_layout . items ( ) : <EOL> if isinstance ( ip_or_ips , str ) : <EOL> if not ip_or_ips in all_ips : <EOL> all_ips . append ( ip_or_ips ) <EOL> elif isinstance ( ip_or_ips , list ) : <EOL> for ip in ip_or_ips : <EOL> if not ip in all_ips : <EOL> all_ips . append ( ip ) <EOL> return all_ips <EOL> def can_ssh_to_ip ( self , ip , keyname , is_verbose ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> RemoteHelper . ssh ( ip , keyname , '<STR_LIT>' , is_verbose , user = '<STR_LIT:root>' ) <EOL> return True <EOL> except ShellException : <EOL> return False <EOL> def ssh ( self , node ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> try : <EOL> index = int ( node ) <EOL> except ValueError : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> keyname = contents_as_yaml [ '<STR_LIT>' ] <EOL> else : <EOL> keyname = "<STR_LIT>" <EOL> nodes = self . get_nodes ( keyname ) <EOL> try : <EOL> ip = nodes [ index ] [ '<STR_LIT>' ] <EOL> except IndexError : <EOL> raise AppScaleException ( "<STR_LIT>" + <EOL> str ( index ) + "<STR_LIT>" + str ( len ( nodes ) ) + <EOL> "<STR_LIT>" ) <EOL> command = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> self . get_key_location ( keyname ) , "<STR_LIT>" + ip ] <EOL> subprocess . call ( command ) <EOL> def status ( self ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> command = [ ] <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( contents_as_yaml [ '<STR_LIT>' ] ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> AppScaleTools . describe_instances ( options ) <EOL> def deploy ( self , app ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> command = [ ] <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( contents_as_yaml [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT:test>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in contents_as_yaml and contents_as_yaml [ '<STR_LIT>' ] == True : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( app ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> return AppScaleTools . upload_app ( options ) <EOL> def undeploy ( self , appid ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> command = [ ] <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( contents_as_yaml [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in contents_as_yaml and contents_as_yaml [ '<STR_LIT>' ] == True : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( appid ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> AppScaleTools . remove_app ( options ) <EOL> def get ( self , property_regex ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> command = [ ] <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( contents_as_yaml [ "<STR_LIT>" ] ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( property_regex ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> return AppScaleTools . get_property ( options ) <EOL> def set ( self , property_name , property_value ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> command = [ ] <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( contents_as_yaml [ "<STR_LIT>" ] ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( property_name ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( property_value ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> AppScaleTools . set_property ( options ) <EOL> def tail ( self , node , file_regex ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> try : <EOL> index = int ( node ) <EOL> except ValueError : <EOL> raise TypeError ( "<STR_LIT>" + "<STR_LIT>" ) <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> keyname = contents_as_yaml [ '<STR_LIT>' ] <EOL> else : <EOL> keyname = "<STR_LIT>" <EOL> try : <EOL> with open ( self . get_locations_json_file ( keyname ) ) as f : <EOL> nodes_json_raw = f . read ( ) <EOL> except IOError : <EOL> raise AppScaleException ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> nodes = json . loads ( nodes_json_raw ) <EOL> try : <EOL> ip = nodes [ index ] [ '<STR_LIT>' ] <EOL> except IndexError : <EOL> raise AppScaleException ( "<STR_LIT>" + <EOL> str ( index ) + "<STR_LIT>" + str ( len ( nodes ) ) + <EOL> "<STR_LIT>" ) <EOL> tail = "<STR_LIT>" . format ( file_regex ) <EOL> command = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> self . get_key_location ( keyname ) , "<STR_LIT>" + ip , tail ] <EOL> subprocess . call ( command ) <EOL> def logs ( self , location ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> command = [ ] <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( contents_as_yaml [ "<STR_LIT>" ] ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( location ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> AppScaleTools . gather_logs ( options ) <EOL> def relocate ( self , appid , http_port , https_port ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> command = [ ] <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( contents_as_yaml [ "<STR_LIT>" ] ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( appid ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( str ( http_port ) ) <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( str ( https_port ) ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> AppScaleTools . relocate_app ( options ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> command = [ ] <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> if "<STR_LIT>" in contents_as_yaml : <EOL> os . environ [ "<STR_LIT>" ] = contents_as_yaml [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in contents_as_yaml : <EOL> os . environ [ "<STR_LIT>" ] = contents_as_yaml [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in contents_as_yaml : <EOL> os . environ [ "<STR_LIT>" ] = contents_as_yaml [ "<STR_LIT>" ] <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> command . append ( "<STR_LIT>" ) <EOL> command . append ( contents_as_yaml [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in contents_as_yaml and contents_as_yaml [ '<STR_LIT>' ] == True : <EOL> command . append ( "<STR_LIT>" ) <EOL> if '<STR_LIT:test>' in contents_as_yaml and contents_as_yaml [ '<STR_LIT:test>' ] == True : <EOL> command . append ( "<STR_LIT>" ) <EOL> options = ParseArgs ( command , "<STR_LIT>" ) . args <EOL> AppScaleTools . terminate_instances ( options ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> contents = self . read_appscalefile ( ) <EOL> contents_as_yaml = yaml . safe_load ( contents ) <EOL> if '<STR_LIT>' not in contents_as_yaml : <EOL> raise BadConfigurationException ( "<STR_LIT>" "<STR_LIT>" ) <EOL> if '<STR_LIT>' in contents_as_yaml and contents_as_yaml [ '<STR_LIT>' ] == True : <EOL> is_verbose = contents_as_yaml [ '<STR_LIT>' ] <EOL> else : <EOL> is_verbose = False <EOL> if '<STR_LIT>' in contents_as_yaml : <EOL> keyname = contents_as_yaml [ '<STR_LIT>' ] <EOL> else : <EOL> keyname = '<STR_LIT>' <EOL> all_ips = self . get_all_ips ( contents_as_yaml [ "<STR_LIT>" ] ) <EOL> if '<STR_LIT:test>' not in contents_as_yaml or contents_as_yaml [ '<STR_LIT:test>' ] != True : <EOL> LocalState . ensure_user_wants_to_terminate ( ) <EOL> for ip in all_ips : <EOL> RemoteHelper . ssh ( ip , keyname , self . TERMINATE , is_verbose ) <EOL> LocalState . cleanup_appscale_files ( keyname ) <EOL> AppScaleLogger . success ( "<STR_LIT>" ) <EOL> return all_ips <EOL> def register ( self , deployment_id ) : <EOL> """<STR_LIT>""" <EOL> appscale_yaml = yaml . safe_load ( self . read_appscalefile ( ) ) <EOL> if '<STR_LIT>' in appscale_yaml : <EOL> keyname = appscale_yaml [ '<STR_LIT>' ] <EOL> else : <EOL> keyname = '<STR_LIT>' <EOL> nodes = self . get_nodes ( keyname ) <EOL> head_node = self . get_head_node ( nodes ) <EOL> if RegistrationHelper . appscale_has_deployment_id ( head_node , keyname ) : <EOL> existing_id = RegistrationHelper . get_deployment_id ( head_node , keyname ) <EOL> if existing_id != deployment_id : <EOL> raise AppScaleException ( <EOL> '<STR_LIT>' ) <EOL> if '<STR_LIT>' in appscale_yaml : <EOL> deployment_type = '<STR_LIT>' <EOL> else : <EOL> deployment_type = '<STR_LIT>' <EOL> deployment = RegistrationHelper . update_deployment ( deployment_type , nodes , <EOL> deployment_id ) <EOL> RegistrationHelper . set_deployment_id ( head_node , keyname , deployment_id ) <EOL> AppScaleLogger . success ( <EOL> '<STR_LIT>' <EOL> . format ( deployment [ '<STR_LIT:name>' ] ) ) </s>
