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): (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 (),() stack_id = parsed_args.stack_id print 'Pulling %s from remote ....\n' % stack_id (err, result) = rpc.stack_info(username, session_id, key_id, None, [stack_id]) if err: print('Pull stack failed') utils.hanlde_error(err,result) else: if len(result) == 0: self.app.stdout.write('The stack does not exist\n') return (),() self.log.debug('> pull stack %s succeed\n' % stack_id ) stack_json = result[0] del stack_json['layout'] del stack_json['property'] #generate yaml app = { 'name' : stack_json['name'], 'region': stack_json['region'], 'hosts' : {}, 'hosts_table' : {}, } for (uid,comp) in stack_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) stack_yaml = utils.dict2yaml(app) stack_file = os.path.join(os.getcwd(), '%s.yaml' % stack_id) with open(stack_file,'w+') as f: f.writelines( stack_yaml ) self.log.debug( '\n> docker state info' ) self.log.debug( '==============================================================' ) self.log.debug( stack_yaml ) self.log.debug( '==============================================================' ) self.app.stdout.write( '%s is saved to %s\n' % (stack_id, stack_file) ) print 'Done!'