def take_action(self, parsed_args): app_id = parsed_args.app_id #get app data from local db (appname,app) = db.get_app_data( app_id ) if not (appname and app): raise RuntimeError('Can not find local app {0}'.format(app_id)) self.log.debug( '==============================================================' ) self.log.debug("> found app %s in local db" % appname) self.log.debug("> app_data") self.log.debug( json.dumps(app, indent=4) ) self.log.debug( '==============================================================' ) config = utils.gen_config(appname) if parsed_args.local: is_succeed = False try: #1. check app state state = db.get_app_state(appname) if not parsed_args.force and state != constant.STATE_APP_STOPPED: raise RuntimeError("App current state is {0}, only support stop 'Stopped' app!".format(state)) print 'Starting local app ...' # #2. update to starting # db.start_app(appname) #3. do action self.start_app(config, appname, app) # #4. update to running # db.start_app(appname,True) print 'Local app %s started!' % appname #save app info into local db stack_id = db.get_stackid_from_appid(app_id) db.create_app( config["appname"], config["appname"], stack_id, '', base64.b64encode(utils.dict2str(app)) ) is_succeed = True except Result,e: print '!!!Expected error occur %s' % str(e.format()) except Exception,e: print '!!!Unexpected error occur %s' % str(e)
def take_action(self, parsed_args): app_id = parsed_args.app_id #get app data from local db (appname,app) = db.get_app_data( app_id ) if not (appname and app): raise RuntimeError('Can not find local app {0}'.format(app_id)) self.log.debug( '==============================================================' ) self.log.debug("> found app %s in local db" % appname) self.log.debug("> app_data") self.log.debug( json.dumps(app, indent=4) ) self.log.debug( '==============================================================' ) config = utils.gen_config(appname) if parsed_args.local: is_succeed = False try: #1. check app state state = db.get_app_state(appname) if state in [constant.STATE_APP_TERMINATED,constant.STATE_APP_TERMINATING]: raise RuntimeError("App current state is {0}, cancel!".format(state)) elif not parsed_args.force and not state in [constant.STATE_APP_RUNNING,constant.STATE_APP_STOPPED]: raise RuntimeError("App current state is {0}, only support stop 'Running' or 'Stopped' app!".format(state)) print 'Terminating local app ...' #2. update to terminating db.terminate_app(appname) #3. do action self.terminate_app(config, appname, app) #4. update to terminated db.terminate_app(appname,True) print 'Local app %s terminated!' % appname is_succeed = True except Result,e: print '!!!Expected error occur %s' % str(e.format()) except Exception,e: print '!!!Unexpected error occur %s' % str(e)
def take_action(self, parsed_args): print 'Show remote app info....' (username, session_id) = utils.load_session() if not(username and session_id): return (),() (project_name, project_id, key_id) = utils.load_current_project() if not key_id: return (),() app_id = parsed_args.app_id print 'Pulling %s from remote ....\n' % app_id (err, result) = rpc.app_info(username, session_id, key_id, None, [app_id]) if err: utils.hanlde_error(err,result) else: if len(result) == 0: print('The app does not exist\n') return (),() self.log.debug('> pull app %s succeed\n' % app_id ) app_json = result[0] del app_json['layout'] del app_json['property'] #generate yaml app = { 'name' : app_json['name'], 'region': app_json['region'], 'hosts' : {}, 'hosts_table' : {}, } for (uid,comp) in app_json['component'].items(): if unicode(comp['type']) == constant.RESTYPE['INSTANCE']: app['hosts_table'][uid] = comp['name'] log_str = '> found instance {0}'.format(comp['name']) if comp['state']: log_str+=': has %s state(s)' % len(comp['state']) hostname = comp['name'] container = {} for (idx,state) in enumerate(comp['state']): state_type = state['module'] if state_type == 'linux.docker.deploy': container_name = state['parameter']['container'] if not container.has_key(state_type): container[state_type] = {} container[state_type][container_name] = state['parameter'] app['hosts'][hostname] = container else: log_str+=': has no state' self.log.debug(log_str) app_yaml = utils.dict2yaml(app) app_file = os.path.join(os.getcwd(), '%s.yaml' % app_id) with open(app_file,'w+') as f: f.writelines( app_yaml ) self.log.debug( '\n> docker state info' ) self.log.debug( '==============================================================' ) self.log.debug( app_yaml ) self.log.debug( '==============================================================' ) self.app.stdout.write( '> %s is saved to %s\n' % (app_id, app_file) ) print 'Done!' try: self.log.debug( "> load data from %s" % app_file ) stream = open(app_file, 'r') app = yaml.load(stream) except Exception: raise RuntimeError('Load yaml error!') config = utils.gen_config(app.get("name","default-app")) is_succeed = False try: app['src_app_id'] = app_id app["name"] = config["appname"] self.clone_app(config, app) #insert app to local db db.create_app(config["appname"], config["appname"], app_id, app['region'], base64.b64encode(utils.dict2str(app)) ) is_succeed = True except Result,e: print '!!!Expected error occur %s' % str(e.format()) except Exception,e: print '!!!Unexpected error occur %s' % str(e)
def take_action(self, parsed_args): stack_id = parsed_args.stack_id stack_file = os.path.join(os.getcwd(), '%s.yaml' % stack_id) if not os.path.isfile(stack_file): print( '%s does not exist, please pull stack first!' % stack_file ) return if parsed_args.run_stack_local: print 'Deploying %s.yaml ......' % stack_id else: print 'Deploying %s.yaml to remote (not support yet, please try -l)....' % stack_id return try: self.log.debug( "> load data from %s" % stack_file ) stream = open(stack_file, 'r') app = yaml.load(stream) except Exception: raise RuntimeError('load yaml error!') if not app: raise RuntimeError('stack json is invalid!') self.log.debug( '==============================================================' ) self.log.debug( json.dumps(app, indent=4) ) self.log.debug( '==============================================================' ) # #generate app_id # app_id = 'app-%s' % str(uuid.uuid4())[:8] # # config = { # "app_id" : app_id, # "interactive": True, # "config_path": os.path.expanduser("~/.visualops"), # "boot2docker_iso": "https://s3.amazonaws.com/visualops-cli/boot2docker.iso", # "volumes": { # "hostname": { # "container": { # "/foo": "/bar", # }, # }, # }, # "cpu_shares": { # "hostname": { # "container": "1", # }, # }, # "mem_limit": { # "hostname": { # "container": "512m", # }, # }, # "chroot": "/path", # "port_bindings": { # "hostnameA": { # "containerA": { # "0.0.0.0:80": "80/tcp", # "6666": "6666/udp", # "127.0.0.1:7777": "7777", # "9999": "9999/tcp", # "23": "23", # }, # }, # }, # } config = utils.gen_config(app.get("name","default-app")) is_succeed = False try: app['stack_id'] = stack_id app["name"] = config["appname"] run_stack(config, app) #save app info into local db db.create_app( config["appname"], config["appname"], stack_id, '', base64.b64encode(utils.dict2str(app)) ) is_succeed = True except Result,e: print '!!!Expected error occur %s' % str(e.format())