Esempio n. 1
0
def pc(request, cc):
    p = cc.client.create_project(name='test-' + random_str(),
                                 clusterId=cc.cluster.id)
    p = cc.client.wait_success(p)
    assert p.state == 'active'
    request.addfinalizer(lambda: cc.client.delete(p))
    url = p.links['self'] + '/schemas'
    return ProjectContext(
        cc, p, cattle.Client(url=url, verify=False, token=cc.client._token))
Esempio n. 2
0
def cc(url, auth_url, chngpwd):
    requests.post(chngpwd, json={
        'newPassword': '******',
    })
    r = requests.post(auth_url, json={
        'username': '******',
        'password': '******',
        'responseType': 'json',
    })
    client = cattle.Client(url=url, token=r.json()['token'])
    cluster = client.by_id_cluster('local')
    return ClusterContext(cluster, client)
Esempio n. 3
0
def mc(url, auth_url, chngpwd):
    requests.post(chngpwd, json={
        'newPassword': '******',
    }, verify=False)
    r = requests.post(auth_url,
                      json={
                          'username': '******',
                          'password': '******',
                          'responseType': 'json',
                      },
                      verify=False)
    client = cattle.Client(url=url, token=r.json()['token'], verify=False)
    return ManagementContext(client)
Esempio n. 4
0
    def __init__(self, url=None, key=None, secret=None):
        """
        Init the connexion on Rancher API
        :param url: the Rancher URL to access on API
        :param key: The rancher key to access on API
        :param secret: The Rancher secret to access on API
        :type url: str
        :type key: str
        :type secret: str
        """

        logger.debug("url: %s", url)
        logger.debug("key: %s", key)
        logger.debug("secret: xxxx")

        self._client = cattle.Client(url=url, access_key=key, secret_key=secret)
Esempio n. 5
0
    FQDN = os.environ['RANCHER_HOST_FQDN']
else:
    HOSTNAME = socket.gethostname()
    FQDN = socket.getfqdn()

print('Hostname of host being removed: ' + HOSTNAME)
print('RANCHER_URL=' + os.environ['RANCHER_URL'] + '/v' +
      str(RANCHER_API_VERSION))

for k in ['http_proxy', 'https_proxy', 'no_proxy']:
    if os.environ.has_key(k):
        print(k + '=' + os.environ[k])

try:
    client = cattle.Client(url=os.environ['RANCHER_URL'] + '/v' +
                           str(RANCHER_API_VERSION),
                           access_key=os.environ['RANCHER_ACCESS_KEY'],
                           secret_key=os.environ['RANCHER_SECRET_KEY'])
except ValueError:
    print("I'm sorry Dave.")

print('Finding host...')

# for some reason this doesn't work:
#   hosts = client.list_host(hostname=HOSTNAME)
hosts = client.list_host()
print("%s hosts exist" % str(len(hosts)))
# client side filter
host = [e for e in hosts if e.get('hostname', None) in (HOSTNAME, FQDN)]

if len(hosts) > 0:
    print("Found Rancher host '%s' (%s)" %
Esempio n. 6
0
    url_parsed = urlparse(os.environ['RANCHER_URL'])
    api_version = url_parsed.path.split('/')[1]
    proto = url_parsed.scheme
    host = url_parsed.netloc
    RANCHER_URL = '{}://{}/{}'.format(proto, host, api_version)
else:
    if '/v' in os.environ['RANCHER_URL']:
        RANCHER_URL = os.environ['RANCHER_URL']
    else:
        RANCHER_URL = urljoin(os.environ['RANCHER_URL'], '/' + RANCHER_API_VERSION)

print('RANCHER_URL: ' + RANCHER_URL)
print('RANCHER_ACCESS_KEY: ' + os.environ['RANCHER_ACCESS_KEY'])

client = cattle.Client(url=RANCHER_URL,
                       access_key=os.environ['RANCHER_ACCESS_KEY'],
                       secret_key=os.environ['RANCHER_SECRET_KEY'])

registration_tokens = client.list_registrationToken()
found_token = False

# get the token for the specific project, otherwise first returned
if 'PROJECT_ID' in locals() and PROJECT_ID is not None:
    print('PROJECT_ID: ' + PROJECT_ID)
    if DEBUG:
        print('Registration tokens returned: ' + str(registration_tokens))

    for t in registration_tokens:
        if t['accountId'] == PROJECT_ID:
            found_token = True
            token = t
def main():
    parser = argparse.ArgumentParser(
        description='Set labels on a Rancher host')
    parser.add_argument('--host',
                        type=str,
                        default=socket.gethostname(),
                        help='Rancher host to set labels for')
    parser.add_argument('--url',
                        type=str,
                        required=True,
                        help='Rancher url to use')
    parser.add_argument('--accesskey',
                        type=str,
                        required=True,
                        help='Rancher API Key to use')
    parser.add_argument('--secretkey',
                        type=str,
                        required=True,
                        help='Rancher API Secret Key to use')
    parser.add_argument('--label',
                        type=str,
                        required=True,
                        action='append',
                        help='Label to set e.g: --label foo=bar this ')
    parser.add_argument('--env',
                        type=str,
                        default=None,
                        help='Rancher environment to use')
    parser.add_argument(
        '--timeout',
        type=str,
        default=600,
        help=
        'Timeout in seconds to wait for host to be active. default 600 seconds'
    )
    parser.add_argument(
        '--retry',
        type=str,
        default=10,
        help=
        'retry time in seconds waiting for a host to be active. default 10 seconds'
    )
    parser.add_argument(
        '--cleanmissing',
        action='store_true',
        help=
        'this will remove labels on a host that are not supplied with the --label argument. defaults to false'
    )
    #parser.add_argument('--env',       type=str, default=None, help='Rancher environment to use')

    args = parser.parse_args()

    # When only a single --label passed we get a string else a list
    if isinstance(args.label, basestring):
        labels = [args.label]
    else:
        labels = args.label

    print('Setting labels on a Rancher host:')

    rancher_full_url = '{}/v{}'.format(args.url, str(RANCHER_API_VERSION))
    print 'RANCHER_URL: {}'.format(rancher_full_url)

    client = cattle.Client(
        url=rancher_full_url,
        access_key=args.accesskey,
        secret_key=args.secretkey,
    )

    print 'RANCHER_HOST_NAME: {}'.format(args.host)

    epoch = time.time() + args.timeout
    cattle_host = wait_host_active(client, args.host, epoch, args.timeout,
                                   args.retry)

    # a dict of labels that will be saved back to the host
    cur_labels = cattle_host['labels']

    print 'Current labels: {}'.format(cur_labels)

    # Generate dict with supplied labels
    new_labels = {}
    for l in labels:
        (k, v) = l.split('=', 2)
        new_labels[k] = unicode(v)

    changed = False
    for i in new_labels:
        if i in cur_labels:
            if new_labels[i] != cur_labels[i]:
                print 'INFO: {} has a changed value from: {} to: {}'.format(
                    i, cur_labels[i], new_labels[i])
                changed = True
        else:
            print 'INFO: {} is a new label'.format(i)
            changed = True

    for k, v in cur_labels.iteritems():
        if k not in new_labels:
            if args.cleanmissing:
                print "Found label on host thats not defined here...removing since youve specified to clean up these"
                changed = True
            else:
                new_labels[k] = v

    print 'Saving labels: {}'.format(new_labels)
    # finally, get the individual host and update it's labels
    if changed:
        h = client.by_id_host(cattle_host['physicalHostId'])
        client.update(h, labels=new_labels)
    else:
        print "Nothing has changed...skipping update"
Esempio n. 8
0
def up(cluster, token):
    c_url = cluster.links['self'] + '/schemas'
    c_client = cattle.Client(url=c_url, token=token, verify=False)
    return c_client
Esempio n. 9
0
def get_project_client_for_token(project, token):
    p_url = project.links['self'] + '/schemas'
    p_client = cattle.Client(url=p_url, token=token, verify=False)
    return p_client
Esempio n. 10
0
def get_client_for_token(token):
    return cattle.Client(url=CATTLE_API_URL, token=token, verify=False)
Esempio n. 11
0
def get_admin_client():
    return cattle.Client(url=CATTLE_API_URL, token=ADMIN_TOKEN, verify=False)
Esempio n. 12
0
def main():
    # setup `logging` module
    logger = logging.getLogger('Rancher Deployment')
    # logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(message)s")  # same as default

    # setup `RainbowLoggingHandler`
    handler = RainbowLoggingHandler(sys.stderr,
        color_funcName=('black', 'yellow', True),
        color_module=('yellow', None, False))

    handler.setFormatter(formatter)
    logger.addHandler(handler)

    # ###################################
    # Importing Args Modules
    # -----------------------------------
    from src import arguments

    # #####################################################
    # 1. Confirming Command Config and Required Arguments
    # -----------------------------------------------------
    # Check to see if arguments have been passed at all
    if arguments.noSystemArgsExist(sys.argv):
        arguments.printHelpDocumentationThenExit()

    # Check if we are printing out the version information
    if arguments.isVersionCommandEntered(sys.argv):
        arguments.printVersionInformationThenExit()

    # Check for the existance of flags
    if arguments.doFlagsExist(sys.argv):
        flags = sys.argv[1]
        arguments.checkHelpFlag(flags)
        FORCE_MODE = arguments.setForceFlag(flags)
        VERBOSE_MODE = arguments.setVerboseFlag(flags)
        DEVELOPMENT_MODE = arguments.setDevelopmentFlag(flags)
    else:
        FORCE_MODE = False
        VERBOSE_MODE = False
        DEVELOPMENT_MODE = False

    if VERBOSE_MODE:
        logger.setLevel(logging.DEBUG)

    logger.info("INFO: Flag Configuration Set")
    logger.debug("DEBUG: Force Mode: %s", FORCE_MODE)
    logger.debug("DEBUG: Verbose Mode: %s", VERBOSE_MODE)
    logger.debug("DEBUG: Development Mode: %s\n", DEVELOPMENT_MODE)

    if not DEVELOPMENT_MODE:
        arguments.checkArgumentStructure(sys.argv)
        ENV_ARGUMENT = arguments.setEnvironment(sys.argv)
        RANCHER_URL = arguments.setRancherUrl(sys.argv)
        RANCHER_ACCESS_KEY = arguments.setRancherKey(sys.argv)
        RANCHER_SECRET_KEY = arguments.setRancherSecret(sys.argv)
    else:
        logger.info("INFO: Currently In Development Mode. Setting Default Parameters.")
        ENV_ARGUMENT = "staging"
        RANCHER_URL = 'http://localhost:8080/v1/'
        RANCHER_ACCESS_KEY = '9F68C78100A2CAA209EC'
        RANCHER_SECRET_KEY = 'pEkMsBYjcZNxhY4rzYuEfdLLj7mDBZ8EPYwbtgVZ'

    if not FORCE_MODE:
        print "Rancher Arguments Set"
        print "ENVIRONMENT: %s" % ENV_ARGUMENT
        logger.debug("DEBUG: RANCHER_URL: %s", RANCHER_URL)
        logger.debug("DEBUG: RANCHER_ACCESS_KEY: %s", RANCHER_ACCESS_KEY)
        logger.debug("DEBUG: RANCHER_SECRET_KEY: %s", RANCHER_SECRET_KEY)
        print "Would you like to continue?"
        var = raw_input("Please enter (Y|N): ")
        if var == "y" or var == "Y":
            print "User Confirmation Accepted. Performing Rancher Deployment"
            logger.debug("DEBUG: Please use the [-f] flag to force application execution and skip confirmation")
        elif var == "n" or var == "N":
            logger.error("ERROR: User stopped app execution.")
            logger.debug("DEBUG: Please use the [-f] flag to force application execution and skip confirmation")
            print sys.exit(0)
        else:
            logger.error("ERROR: Invalid User Input")
            logger.error("ERROR: Please use the [-f] flag to force application execution and skip confirmation")
            print sys.exit(0)
    else:
        logger.info("INFO: Force Mode Enabled. Skipping Flag Confirmation")


    print "Starting Matador Deploy..."
    # ##################################
    # Import Additional Custom Modules
    # ----------------------------------
    # NOTE: This is done here so that the global vars can be used in the inner modules
    from src import yml_reader
    from src import compose_builder
    from src import rancher_compose

    # ##################################
    # 2. Reading YAML Files Into Script
    # ----------------------------------
    rancher_compose_list = yml_reader.readRancherComposeTemplate()
    docker_compose_list = yml_reader.readDockerComposeTemplate()
    config_file = yml_reader.readConfigurationFile()
    global_config = yml_reader.getGlobalConfig()
    env_config = yml_reader.getEnvConfig(ENV_ARGUMENT)
    PROJECT_NAME = config_file['project_name'] + "-" + ENV_ARGUMENT

    # ##################################################
    # 3. Combine config into the rancher compose
    # --------------------------------------------------
    compose_builder.addConfigToDockerCompose(docker_compose_list, global_config)
    compose_builder.addConfigToDockerCompose(docker_compose_list, env_config)

    # ###############################################
    # 4. Set the image for the deployment
    # -----------------------------------------------
    compose_builder.setImageForDockerConfig(docker_compose_list, ENV_ARGUMENT, config_file['image_base'])

    # ###############################################
    # 5. Save new yml out to a temp file
    # -----------------------------------------------
    yml_reader.createBuildDirectory()
    yml_reader.saveRancherComposeFile(rancher_compose_list)
    yml_reader.saveDockerComposeFile(docker_compose_list)

    # ###############################################
    # 6. Start updating this stuff to rancher baby
    # -----------------------------------------------
    cattle_client = cattle.Client(
        url=RANCHER_URL,
        access_key=RANCHER_ACCESS_KEY,
        secret_key=RANCHER_SECRET_KEY
    )
    rancher_compose.setRancherVars(RANCHER_URL, RANCHER_ACCESS_KEY, RANCHER_SECRET_KEY, PROJECT_NAME)
    rancher_compose.checkForExistingEnvironment(cattle_client, PROJECT_NAME)
    rancher_compose.pushToRancher()
Esempio n. 13
0
def cc(mc):
    cluster = mc.client.by_id_cluster('local')
    url = cluster.links['self'] + '/schemas'
    client = cattle.Client(url=url, verify=False, token=mc.client._token)
    return ClusterContext(mc, cluster, client)
def write(config_dir, print, cattle_url, cattle_access_key, cattle_secret_key):
    client = cattle.Client(url=cattle_url,
                           access_key=cattle_access_key,
                           secret_key=cattle_secret_key)
    hosts = []
    cadvisors = []
    rancher = []
    for host in client.list('host'):
        host_ip = host.data.fields.agentIpAddress
        for instance in host.instances():
            if instance.state != 'running':
                continue
            if 'node-exporter' in instance.name:
                click.echo("Discovered node exporter on {}"
                           .format(host.hostname))
                hostname = (instance.primaryIpAddress or host_ip)
                hosts.append({
                    'targets': ['{}:{}'.format(hostname, 9100)],
                    'labels': {
                        'instance': host.hostname,
                        'rancher_env': env_name(client, instance.accountId)
                    }
                })
            elif 'cadvisor' in instance.name:
                click.echo('Discovered cadvisor on {}'.format(host.hostname))
                hostname = (instance.primaryIpAddress or host_ip)
                cadvisors.append({
                    'targets': ['{}:{}'.format(hostname, 9001)],
                    'labels': {
                        'instance': host.hostname,
                        'rancher_env': env_name(client, instance.accountId)
                    }
                })
            elif 'rancher-exporter' in instance.name:
                click.echo('Discovered rancher exporter on {}'
                           .format(host.hostname))
                hostname = (instance.primaryIpAddress or host_ip)
                rancher.append({
                    'targets': ['{}:{}'.format(hostname, 9173)],
                    'labels': {
                        'instance': host.hostname,
                        'rancher_env': env_name(client, instance.accountId)
                    }
                })

    # noinspection PyTypeChecker
    config['scrape_configs'].extend([
        {
            'job_name': 'HostMetrics',
            'file_sd_configs': [
                {'files': [os.path.join(config_dir, 'hosts.yml')]}
            ]
        },
        {
            'job_name': 'rancher-api',
            'file_sd_configs': [
                {'files': [os.path.join(config_dir, 'rancher.yml')]}
            ]
        },
        {
            'job_name': 'ContainerMetrics',
            'file_sd_configs': [
                {'files': [os.path.join(config_dir, 'cadvisors.yml')]}
            ]
        }
    ])
    files = (
        ('config.yml', yaml.dump(config, default_flow_style=False)),
        ('hosts.yml', yaml.dump(hosts)),
        ('rancher.yml', yaml.dump(rancher)),
        ('cadvisors.yml', yaml.dump(cadvisors))
    )
    if print:
        for filename, yml in files:
            click.echo('# {}'.format(filename))
            click.echo('---')
            click.echo(yml)
    else:
        for filename, yml in files:
            with open(os.path.join(config_dir, filename), mode='w') as f:
                f.writelines(yml)
            click.echo('... wrote {}'.format(filename))
        click.echo('Config written to {}'.format(config_dir))
Esempio n. 15
0
def _v2_client(client):
    return cattle.Client(access_key=client._access_key,
                         secret_key=client._secret_key,
                         url=client._url.replace('/v1', '/v2-beta'))