Beispiel #1
0
def manageservice(action):
    import arcrest.admin as admin
    args = manageserviceargs.parse_args()
    admin_url, rest_url = get_rest_urls(args.site)
    if args.list:
        with action("checking arguments"):
            assert not args.name, "name cannot be set if listing services"
        with action("connecting to admin site {0}".format(admin_url)):
            site = admin.Admin(admin_url,
                               args.username,
                               args.password,
                               generate_token=True)
            assert site._json_struct.get('status', 'ok') != 'error',\
                   ' '.join(site._json_struct.get('messages',
                       ['Could not connect to site.']))
        with action("listing services"):
            services = site.services
            folders = services.folders
        with action("printing services"):
            status_map = {'stopped': 'stop', 'started': 'started'}
            servicelist = list(services.services)
            for folder in folders:
                servicelist += list(folder.services)
            for service in servicelist:
                print("{0:40} | {1}".format(
                    (service.parent.folderName +
                     "/" if service.parent.folderName != "/" else "") +
                    service.name, service.status['realTimeState']))
    else:
        with action("checking arguments"):
            assert args.name, "Service name not specified"
        with action("connecting to admin site {0}".format(admin_url)):
            site = admin.Admin(admin_url,
                               args.username,
                               args.password,
                               generate_token=True)
            assert site._json_struct.get('status', 'ok') != 'error',\
                   ' '.join(site._json_struct.get('messages',
                       ['Could not connect to site.']))
        with action("listing services"):
            services = site.services
        with action("searching for service %s" % args.name):
            service = services[args.name]
        operation = (args.operation or '').lower()
        if operation == 'status':
            for key, item in sorted(service.status.items()):
                print("{0}: {1}".format(key, item))
        elif operation == 'start':
            with action("starting service"):
                return service.start()
        elif operation == 'stop':
            with action("stopping service"):
                return service.stop()
        elif operation == 'delete':
            with action("deleting service"):
                return service.delete()
def createservice(action):
    import arcrest.admin as admin
    args = createserviceargs.parse_args()
    files = args.sdfile
    admin_url, rest_url = get_rest_urls(args.site)
    with action("connecting to admin site {0}".format(admin_url)):
        site = admin.Admin(admin_url, args.username, args.password,
                           generate_token=args.token)
        assert site._json_struct.get('status', 'ok') != 'error',\
               ' '.join(site._json_struct.get('messages',
                   ['Could not connect to site.']))
    with action("connecting to REST services {0}".format(rest_url)):
        rest_site = Catalog(rest_url, args.username, args.password,
                            generate_token=args.token)
    with action("looking up Publish Tool"):
        publish_tool = (rest_site['System']
                                 ['PublishingTools']
                                 ['Publish Service Definition'])
    with action("looking up cluster"):
        cluster = site.clusters[args.cluster] if args.cluster else None
    with action("verifying service definition file exists"):
        all_files = [os.path.abspath(filename) for filename in files]
        assert all_files, "No file specified"
        for filename in all_files:
            assert os.path.exists(filename) and os.path.isfile(filename), \
                    "{0} is not a file".format(filename)
    ids = []
    publish_tool.__post__ = True
    for filename in all_files:
        with action("uploading and publishing {0}".format(
                                                  os.path.basename(filename))):
            id = site.uploads.upload(filename)['itemID']
            config_url = urlparse.urljoin(site.uploads.url, 
                                     '{}/serviceconfiguration.json'.format(id))
            with action("fetching default configuration"):
                if args.token:
                    config_url += "?token={}".format(site.__token__)
                config_json = json.load(urllib2.urlopen(creportcachestatus.pyonfig_url))
            with action("adjusting service configuration with user options"):
                if args.folder_name and 'folderName' in config_json:
                    config_json['folderName'] = args.folder_name
                if args.service_name and 'service' in config_json \
                        and 'serviceName' in config_json['service']:
                    config_json['service']['serviceName'] = args.service_name
            with action("publishing {0}".format(os.path.basename(filename))):
                new_json = json.dumps(config_json)
                result_object = publish_tool(id, new_json, "")
                wait_on_tool_run(result_object, silent=True)
            with action("deleting temporary {0} on server ({1})".format(
                                                  os.path.basename(filename),
                                                  id)):
                delete_url = urlparse.urljoin(site.uploads.url, 
                                         '{}/delete'.format(id))
                urllib2.urlopen(delete_url, '').read()
def managesite(action):
    import arcrest.admin as admin
    args = managesiteargs.parse_args()
    admin_url, rest_url = get_rest_urls(args.site)
    with action("connecting to admin site {0}".format(admin_url)):
        site = admin.Admin(admin_url, args.username, args.password,
                           generate_token=args.token)
        assert site._json_struct.get('status', 'ok') != 'error',\
               ' '.join(site._json_struct.get('messages',
                   ['Could not connect to site.']))
    with action("determining actions to perform"):
        assert any([
                     args.add_machines, 
                     args.remove_machines,
                     args.delete_cluster,
                     args.create_cluster,
                     args.list,
                     args.list_clusters,
                     args.operation
                ]), "No action specified (use --help for options)"
    operation = (args.operation or '').lower()
    if not args.list_clusters:
        with action("looking up cluster"):
            try:
                cluster = site.clusters[args.cluster] if args.cluster else None
            except KeyError:
                if args.create_cluster:
                    cluster = site.clusters.create(args.cluster)
                else:
                    raise
        with action("performing {0}".format(operation or '')):
            assert cluster, "No cluster specified"
            if operation.lower() == "start":
                cluster.start()
            elif operation.lower() == "stop":
                cluster.stop()
            elif operation == "chkstatus":
                raise NotImplementedError("Chkstatus not implemented")
        with action("deleting cluster"):
            if args.delete_cluster:
                assert cluster, "No cluster specified to delete"
                cluster.delete()
        with action("adding machines to cluster"):
            if args.add_machines:
                assert cluster, "No cluster specified"
                for machine in args.add_machines:
                    with action("adding {0} to cluster".format(machine)):
                        cluster.machines.add(machine)
        with action("removing machines from cluster"):
            if args.remove_machines:
                assert cluster, "No cluster specified"
                for machine in args.remove_machines:
                    with action("deleting {0} from cluster".format(machine)):
                        cluster.machines.remove(machine)
        with action("listing machines"):
            if args.list:
                name, itemobject = ('cluster', cluster) \
                        if cluster else ('site', site)
                print("===Machines on this {0}===".format(name))
                for machine in itemobject.machines.keys():
                    print("-", machine)
                print()
    elif args.list_clusters:
        with action("listing clusters"):
            if args.list_clusters:
                print("===Clusters on this site===")
                for cluster in site.clusters.clusterNames:
                    print("-", cluster)
                print()