예제 #1
0
파일: pull.py 프로젝트: VisualOps/cli
    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!'
예제 #2
0
파일: info.py 프로젝트: VisualOps/cli
    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 (),()

        # get stack info
        stack_id = parsed_args.stack_id
        (err, result) = rpc.stack_info(username, session_id, key_id, None, [ stack_id ])

        if err:
            print('Get stack info failed')
            utils.hanlde_error(err,result)
        else:
            self.log.debug('> get {0} stack(s) info'.format(len(result)))

            if len(result) == 0:
                return (),()

            stack_json = result[0]

            del stack_json['layout']
            del stack_json['property']

            instance_with_state    = 0
            instance_without_state = 0
            for (uid,comp) in stack_json['component'].items():
                if unicode(comp['type']) == constant.RESTYPE['INSTANCE']:

                    log_str = '> found instance {0}'.format(comp['name'])

                    if comp['state']:
                        log_str+=': has %s state(s)' % len(comp['state'])
                        instance_with_state+=1
                    else:
                        log_str+=': has no state'
                        instance_without_state+=1

                    self.log.debug(log_str)

            print "Stack Info in %s(%s):" % (project_name,project_id)
            columns = ( 'Id',
                        'Name',
                        'Region',
                        'Version',
                        'Module Tag',
                        'Component',
                        'Instance Total',
                        'Instance With State',
                        'Instance Without State',
                       )
            data = (
                    result[0]['id'],
                    result[0]['name'],
                    result[0]['region'],
                    result[0]['version'],
                    result[0]['agent']['module']['tag'],
                    len(result[0]['component']),
                    instance_with_state+instance_without_state,
                    instance_with_state,
                    instance_without_state,
                    )
            return (columns, data)