コード例 #1
0
ファイル: utils.py プロジェクト: bac/juju-gui-charm
def write_builtin_server_startup(
        ssl_cert_path, serve_tests=False, sandbox=False,
        builtin_server_logging='info', insecure=False, charmworld_url='',
        env_password=None, env_uuid=None, juju_version=None, debug=False,
        port=None, jem_location=None, interactive_login=False):
    """Generate the builtin server Upstart file."""
    log('Generating the builtin server Upstart file.')
    context = {
        'builtin_server_logging': builtin_server_logging,
        'insecure': insecure,
        'sandbox': sandbox,
        'serve_tests': serve_tests,
        'ssl_cert_path': ssl_cert_path,
        'charmworld_url': charmworld_url,
        'http_proxy': os.environ.get('http_proxy'),
        'https_proxy': os.environ.get('https_proxy'),
        'no_proxy': os.environ.get('no_proxy', os.environ.get('NO_PROXY')),
        'juju_gui_debug': debug,
        'env_password': env_password,
        'env_uuid': env_uuid,
        'juju_version': juju_version,
        'port': port,
        'jem_location': jem_location,
        'interactive_login': interactive_login,
    }
    if not sandbox:
        api_url = 'wss://{}'.format(get_api_address())
        context.update({
            'api_url': api_url,
            'api_version': 'go',
        })
    if serve_tests:
        context['tests_root'] = os.path.join(JUJU_GUI_DIR, 'test', '')
    render_to_file(
        'guiserver.conf.template', context, GUISERVER_INIT_PATH)
コード例 #2
0
def write_builtin_server_startup(gui_root,
                                 ssl_cert_path,
                                 serve_tests=False,
                                 sandbox=False,
                                 builtin_server_logging='info',
                                 insecure=False,
                                 charmworld_url='',
                                 port=None):
    """Generate the builtin server Upstart file."""
    log('Generating the builtin server Upstart file.')
    context = {
        'builtin_server_logging': builtin_server_logging,
        'gui_root': gui_root,
        'insecure': insecure,
        'sandbox': sandbox,
        'serve_tests': serve_tests,
        'ssl_cert_path': ssl_cert_path,
        'charmworld_url': charmworld_url,
        'http_proxy': os.environ.get('http_proxy'),
        'https_proxy': os.environ.get('https_proxy'),
        'no_proxy': os.environ.get('no_proxy', os.environ.get('NO_PROXY')),
        'port': port,
    }
    if not sandbox:
        api_url = 'wss://{}'.format(get_api_address())
        context.update({
            'api_url': api_url,
            'api_version': 'go',
        })
    if serve_tests:
        context['tests_root'] = os.path.join(JUJU_GUI_DIR, 'test', '')
    render_to_file('guiserver.conf.template', context, GUISERVER_INIT_PATH)
コード例 #3
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def stop_improv():
    """Stop a simulated Juju environment."""
    log('Stopping the staging backend.')
    with su('root'):
        service_control(IMPROV, STOP)
    log('Removing the staging Upstart script.')
    cmd_log(run('rm', '-f', IMPROV_INIT_PATH))
コード例 #4
0
ファイル: utils.py プロジェクト: bac/juju-gui-charm
def setup_ports(previous_port, current_port):
    """Open or close ports based on the supplied ports.

    The given ports specify the previously provided and the current value.
    They can be int numbers if the ports are specified, None otherwise, in
    which case the default ones (80 and 443) are used.
    """
    # If a custom port was previously defined we want to make sure we close it.
    if previous_port is not None and port_in_range(previous_port):
        log('Closing user provided port {}.'.format(previous_port))
        close_port(previous_port)
    if current_port is not None:
        if port_in_range(current_port):
            # Ensure the default ports are closed when setting the custom one.
            log('Closing default ports 80 and 443.')
            close_port(80)
            close_port(443)
            # Open the custom defined port.
            log('Opening user provided port {}.'.format(current_port))
            open_port(current_port)
            return
        log('Ignoring provided port {}: not in range.'.format(current_port))
    log('Opening default ports 80 and 443.')
    open_port(80)
    open_port(443)
コード例 #5
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def setup_haproxy_config(ssl_cert_path, secure=True):
    """Generate the haproxy configuration file."""
    log('Setting up haproxy Upstart file.')
    config_path = os.path.join(CONFIG_DIR, 'haproxy.conf')
    shutil.copy(config_path, SYS_INIT_DIR)
    log('Generating haproxy configuration file.')
    is_legacy_juju = legacy_juju()
    if is_legacy_juju:
        # The PyJuju API agent is listening on localhost.
        api_address = '127.0.0.1:{}'.format(API_PORT)
    else:
        # Retrieve the juju-core API server address.
        api_address = get_api_address()
    context = {
        'api_address': api_address,
        'api_pem': JUJU_PEM,
        'legacy_juju': is_legacy_juju,
        'ssl_cert_path': ssl_cert_path,
        # In PyJuju environments, use the same certificate for both HTTPS and
        # WebSocket connections. In juju-core the system already has the proper
        # certificate installed.
        'web_pem': JUJU_PEM,
        'web_port': WEB_PORT,
        'secure': secure
    }
    render_to_file('haproxy.cfg.template', context, HAPROXY_CFG_PATH)
コード例 #6
0
 def start(self, backend):
     log('Starting Juju GUI.')
     config = backend.config
     build_dir = utils.compute_build_dir(config['juju-gui-debug'],
                                         config['serve-tests'])
     # Check if a secure WebSocket client connection is required.
     secure = config.get('ws-secure')
     if secure is None:
         # WebSocket security is not explicitly provided: use secure
         # WebSocket if the GUI server is running in secure mode.
         secure = config['secure']
     utils.write_gui_config(
         config['juju-gui-console-enabled'],
         config.get('login-help'),
         config['read-only'],
         config['charmworld-url'],
         config['charmstore-url'],
         build_dir,
         secure=secure,
         sandbox=config['sandbox'],
         cached_fonts=config['cached-fonts'],
         ga_key=config['ga-key'],
         hide_login_button=config['hide-login-button'],
         juju_core_version=config.get('juju-core-version'),
         password=config.get('password'),
         juju_env_uuid=os.getenv('JUJU_ENV_UUID', None))
     # Set up TCP ports.
     previous_port = backend.prev_config.get('port')
     current_port = backend.config.get('port')
     utils.setup_ports(previous_port, current_port)
コード例 #7
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def write_builtin_server_startup(
        gui_root, ssl_cert_path, serve_tests=False, sandbox=False,
        builtin_server_logging='info', insecure=False, charmworld_url=''):
    """Generate the builtin server Upstart file."""
    log('Generating the builtin server Upstart file.')
    context = {
        'builtin_server_logging': builtin_server_logging,
        'gui_root': gui_root,
        'insecure': insecure,
        'sandbox': sandbox,
        'serve_tests': serve_tests,
        'ssl_cert_path': ssl_cert_path,
        'charmworld_url': charmworld_url,
    }
    if not sandbox:
        is_legacy_juju = legacy_juju()
        if is_legacy_juju:
            api_url = 'wss://127.0.0.1:{}/ws'.format(API_PORT)
        else:
            api_url = 'wss://{}'.format(get_api_address())
        context.update({
            'api_url': api_url,
            'api_version': 'python' if is_legacy_juju else 'go',
        })
    if serve_tests:
        context['tests_root'] = os.path.join(JUJU_GUI_DIR, 'test', '')
    render_to_file(
        'guiserver.conf.template', context, GUISERVER_INIT_PATH)
コード例 #8
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def stop_agent():
    """Stop the Juju agent."""
    log('Stopping the API agent.')
    with su('root'):
        service_control(AGENT, STOP)
    log('Removing the API agent Upstart script.')
    cmd_log(run('rm', '-f', AGENT_INIT_PATH))
コード例 #9
0
ファイル: backend.py プロジェクト: kadams54/juju-gui-charm
 def start(self, backend):
     log("Starting Juju GUI.")
     config = backend.config
     build_dir = utils.compute_build_dir(config["juju-gui-debug"], config["serve-tests"])
     # Check if a secure WebSocket client connection is required.
     secure = config.get("ws-secure")
     if secure is None:
         # WebSocket security is not explicitly provided: use secure
         # WebSocket if the GUI server is running in secure mode.
         secure = config["secure"]
     utils.write_gui_config(
         config["juju-gui-console-enabled"],
         config.get("login-help"),
         config["read-only"],
         config["charmworld-url"],
         config["charmstore-url"],
         build_dir,
         secure=secure,
         sandbox=config["sandbox"],
         cached_fonts=config["cached-fonts"],
         ga_key=config["ga-key"],
         hide_login_button=config["hide-login-button"],
         juju_core_version=config.get("juju-core-version"),
         password=config.get("password"),
         juju_env_uuid=os.getenv("JUJU_ENV_UUID", None),
     )
     # Set up TCP ports.
     previous_port = backend.prev_config.get("port")
     current_port = backend.config.get("port")
     utils.setup_ports(previous_port, current_port)
コード例 #10
0
def setup_ports(previous_port, current_port):
    """Open or close ports based on the supplied ports.

    The given ports specify the previously provided and the current value.
    They can be int numbers if the ports are specified, None otherwise, in
    which case the default ones (80 and 443) are used.
    """
    # If a custom port was previously defined we want to make sure we close it.
    if previous_port is not None and port_in_range(previous_port):
        log('Closing user provided port {}.'.format(previous_port))
        close_port(previous_port)
    if current_port is not None:
        if port_in_range(current_port):
            # Ensure the default ports are closed when setting the custom one.
            log('Closing default ports 80 and 443.')
            close_port(80)
            close_port(443)
            # Open the custom defined port.
            log('Opening user provided port {}.'.format(current_port))
            open_port(current_port)
            return
        log('Ignoring provided port {}: not in range.'.format(current_port))
    log('Opening default ports 80 and 443.')
    open_port(80)
    open_port(443)
コード例 #11
0
ファイル: backend.py プロジェクト: bac/juju-gui-charm
 def install(self, backend):
     """Install the GUI and dependencies."""
     # If the source setting has changed since the last time this was run,
     # get the code, from either a static release or a branch as specified
     # by the source setting, and install it.
     log('Installing juju gui.')
     utils.setup_gui()
コード例 #12
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def stop_haproxy_apache():
    """Stop the haproxy and Apache services."""
    log('Stopping the haproxy and Apache services.')
    with su('root'):
        service_control(HAPROXY, STOP)
        service_control(APACHE, STOP)
    remove_haproxy_setup()
    remove_apache_setup()
コード例 #13
0
ファイル: backend.py プロジェクト: CiscoSystems/jujucharm-n1k
 def install(self, backend):
     log("Setting up base dir: {}.".format(utils.BASE_DIR))
     try:
         os.makedirs(utils.BASE_DIR)
     except OSError as err:
         # The base directory might already exist: ignore the error.
         if err.errno != errno.EEXIST:
             raise
コード例 #14
0
 def install(self, backend):
     log('Setting up base dir: {}.'.format(utils.BASE_DIR))
     try:
         os.makedirs(utils.BASE_DIR)
     except OSError as err:
         # The base directory might already exist: ignore the error.
         if err.errno != errno.EEXIST:
             raise
コード例 #15
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def download_release(url, filename):
    """Download a Juju GUI release from the given URL.

    Save the resulting file as filename in the local releases repository.
    Return the full path of the saved file.
    """
    destination = os.path.join(RELEASES_DIR, filename)
    log('Downloading release file: {} --> {}.'.format(url, destination))
    cmd_log(run('curl', '-L', '-o', destination, url))
    return destination
コード例 #16
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def start_haproxy_apache(
        build_dir, serve_tests, ssl_cert_path, secure):
    """Set up and start the haproxy and Apache services."""
    log('Setting up Apache and haproxy.')
    setup_apache_config(build_dir, serve_tests)
    setup_haproxy_config(ssl_cert_path, secure)
    log('Starting the haproxy and Apache services.')
    with su('root'):
        service_control(APACHE, RESTART)
        service_control(HAPROXY, RESTART)
コード例 #17
0
def download_release(url, filename):
    """Download a Juju GUI release from the given URL.

    Save the resulting file as filename in the local releases repository.
    Return the full path of the saved file.
    """
    destination = os.path.join(RELEASES_DIR, filename)
    log('Downloading release file: {} --> {}.'.format(url, destination))
    cmd_log(run('curl', '-L', '-o', destination, url))
    return destination
コード例 #18
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def start_builtin_server(
        build_dir, ssl_cert_path, serve_tests, sandbox, builtin_server_logging,
        insecure, charmworld_url):
    """Start the builtin server."""
    write_builtin_server_startup(
        build_dir, ssl_cert_path, serve_tests=serve_tests, sandbox=sandbox,
        builtin_server_logging=builtin_server_logging, insecure=insecure,
        charmworld_url=charmworld_url)
    log('Starting the builtin server.')
    with su('root'):
        service_control(BUILTIN_SERVER, RESTART)
コード例 #19
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def setup_gui(release_tarball):
    """Set up Juju GUI."""
    # Uncompress the release tarball.
    log('Installing Juju GUI.')
    release_dir = os.path.join(BASE_DIR, 'release')
    cmd_log(run('rm', '-rf', release_dir))
    os.mkdir(release_dir)
    uncompress = command('tar', '-x', '-a', '-C', release_dir, '-f')
    cmd_log(uncompress(release_tarball))
    # Link the Juju GUI dir to the contents of the release tarball.
    cmd_log(run('ln', '-sf', first_path_in_dir(release_dir), JUJU_GUI_DIR))
コード例 #20
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def remove_apache_setup():
    """Remove Apache setup."""
    if os.path.exists(APACHE_SITE):
        log('Removing Apache setup.')
        cmd_log(run('rm', '-f', APACHE_SITE))
        with su('root'):
            run('a2dismod', 'headers')
            run('a2dissite', 'juju-gui')
            run('a2ensite', 'default')
        if os.path.exists(APACHE_PORTS):
            cmd_log(run('rm', '-f', APACHE_PORTS))
コード例 #21
0
    def test_log(self):
        # The "log" function forwards a string on to the juju-log command.
        logged = []

        def juju_log(*args):
            logged.append(args)
        charmhelpers.log('This is a log message', juju_log)
        # Since we only logged one message, juju-log was only called once..
        self.assertEqual(len(logged), 1)
        # The message was included in the arguments passed to juju-log.
        self.assertIn('This is a log message', logged[0])
コード例 #22
0
    def test_log(self):
        # The "log" function forwards a string on to the juju-log command.
        logged = []

        def juju_log(*args):
            logged.append(args)

        charmhelpers.log('This is a log message', juju_log)
        # Since we only logged one message, juju-log was only called once..
        self.assertEqual(len(logged), 1)
        # The message was included in the arguments passed to juju-log.
        self.assertIn('This is a log message', logged[0])
コード例 #23
0
def setup_gui(release_tarball):
    """Set up Juju GUI."""
    # Uncompress the release tarball.
    log('Installing Juju GUI.')
    release_dir = os.path.join(BASE_DIR, 'release')
    cmd_log(run('rm', '-rf', release_dir))
    os.mkdir(release_dir)
    uncompress = command('tar', '-x', '-a', '-C', release_dir, '-f')
    cmd_log(uncompress(release_tarball))
    # Link the Juju GUI dir to the contents of the release tarball.
    cmd_log(run('rm', '-rf', JUJU_GUI_DIR))
    cmd_log(run('ln', '-s', first_path_in_dir(release_dir), JUJU_GUI_DIR))
コード例 #24
0
    def test_log_escapes_message(self):
        # The Go version of juju-log interprets any string begining with two
        # hyphens ("--") as a command-line switch, even if the third character
        # is non-alphanumeric.  This is different behavior than the Python
        # version of juju-log.  Therefore we signfiy the end of options by
        # inserting the string " -- " just before the log message.
        logged = []

        def juju_log(*args):
            logged.append(args)
        charmhelpers.log('This is a log message', juju_log)
        # The call to juju-log includes the " -- " string before the message.
        self.assertEqual([('--', 'This is a log message')], logged)
コード例 #25
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def start_improv(staging_env, ssl_cert_path):
    """Start a simulated juju environment using ``improv.py``."""
    log('Setting up the staging Upstart script.')
    context = {
        'juju_dir': JUJU_AGENT_DIR,
        'keys': ssl_cert_path,
        'port': API_PORT,
        'staging_env': staging_env,
    }
    render_to_file('juju-api-improv.conf.template', context, IMPROV_INIT_PATH)
    log('Starting the staging backend.')
    with su('root'):
        service_control(IMPROV, START)
コード例 #26
0
    def test_log_escapes_message(self):
        # The Go version of juju-log interprets any string begining with two
        # hyphens ("--") as a command-line switch, even if the third character
        # is non-alphanumeric.  This is different behavior than the Python
        # version of juju-log.  Therefore we signfiy the end of options by
        # inserting the string " -- " just before the log message.
        logged = []

        def juju_log(*args):
            logged.append(args)

        charmhelpers.log('This is a log message', juju_log)
        # The call to juju-log includes the " -- " string before the message.
        self.assertEqual([('--', 'This is a log message')], logged)
コード例 #27
0
ファイル: utils.py プロジェクト: bac/juju-gui-charm
def setup_gui():
    """Set up Juju GUI."""

    # Install ensuring network access is not used.  All dependencies should
    # already be installed from the deps directory.
    jujugui_deps = os.path.join(CURRENT_DIR, 'jujugui-deps')
    release_tarball_path = get_release_file_path()
    log('Installing Juju GUI from {}.'.format(release_tarball_path))
    cmd = (
        '/usr/bin/pip',  'install', '--no-index',
        '--find-links', 'file:///{}'.format(jujugui_deps),
        release_tarball_path,
    )
    with su('root'):
        cmd_log(run(*cmd))
コード例 #28
0
def install_builtin_server():
    """Install the builtin server code."""
    log('Installing the builtin server dependencies.')
    deps = os.path.join(CURRENT_DIR, 'deps')
    requirements = os.path.join(CURRENT_DIR, 'server-requirements.pip')
    # Install the builtin server dependencies avoiding to download requirements
    # from the network.
    with su('root'):
        cmd_log(
            run('pip', 'install', '--no-index', '--no-dependencies',
                '--find-links', 'file:///{}'.format(deps), '-r', requirements))
    log('Installing the builtin server.')
    setup_cmd = os.path.join(SERVER_DIR, 'setup.py')
    with su('root'):
        cmd_log(run('/usr/bin/python', setup_cmd, 'install'))
コード例 #29
0
def parse_source(source):
    """Parse the ``juju-gui-source`` option.

    Return a tuple of two elements representing info on how to deploy Juju GUI.
    Examples:
       - ('local', None): latest local release;
       - ('stable', None): latest stable release;
       - ('develop', None): latest build from git trunk;
       - ('release', '0.1.0'): release v0.1.0;
       - ('branch', ('https://github.com/juju/juju-gui.git', 'add-feature'):
         release is made from a branch -
         in this case the second element includes the branch or SHA;
       - ('branch', ('https://github.com/juju/juju-gui.git', None): no
         revision is specified;
       - ('url', 'http://example.com/gui.tar.gz'): release from a downloaded
         file.
    """
    def is_url(url_check):
        if ' ' in url_check:
            url_check, _ = url_check.split(' ')

        if url_check.startswith('http') and not url_check.endswith('.git'):
            return True
        else:
            return False

    def is_branch(check_branch):
        target = None
        if ' ' in check_branch:
            check_branch, target = check_branch.split(' ')

        if check_branch.startswith('http') and check_branch.endswith('.git'):
            return (check_branch, target)
        else:
            return False

    if is_url(source):
        return 'url', source

    if source in ('local', 'stable', 'develop'):
        return source, None

    check_branch = is_branch(source)
    if (check_branch):
        return ('branch', check_branch)

    log('Source is defaulting to stable release.')
    return 'stable', source
コード例 #30
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def install_builtin_server():
    """Install the builtin server code."""
    log('Installing the builtin server dependencies.')
    deps = os.path.join(CURRENT_DIR, 'deps')
    requirements = os.path.join(CURRENT_DIR, 'server-requirements.pip')
    # Install the builtin server dependencies avoiding to download requirements
    # from the network.
    with su('root'):
        cmd_log(run(
            'pip', 'install', '--no-index', '--no-dependencies',
            '--find-links', 'file:///{}'.format(deps), '-r', requirements
        ))
    log('Installing the builtin server.')
    setup_cmd = os.path.join(SERVER_DIR, 'setup.py')
    with su('root'):
        cmd_log(run('/usr/bin/python', setup_cmd, 'install'))
コード例 #31
0
def fetch_gui_from_branch(branch_url, revision, logpath):
    """Retrieve the Juju GUI from a branch and build a release archive."""
    # Inject NPM packages into the cache for faster building.
    prime_npm_cache(get_npm_cache_archive_url())

    # Create a release starting from a branch.
    juju_gui_source_dir = os.path.join(CURRENT_DIR, 'juju-gui-source')
    cmd_log(run('rm', '-rf', juju_gui_source_dir))
    git_clone = command('git', 'clone')

    if revision is None:
        log('Retrieving Juju GUI source from {} (default trunk).'.format(
            branch_url))
        cmd_log(git_clone('--depth', '1', branch_url, juju_gui_source_dir))
    elif revision.startswith('@'):
        log('Retrieving Juju GUI source from {} (commit: {}).'.format(
            branch_url, revision[1:]))
        # Retrieve a full clone and then checkout the specific commit.
        git_dir = os.path.join(juju_gui_source_dir, '.git')
        cmd_log(git_clone(branch_url, juju_gui_source_dir))
        cmd_log(
            run('git', '--git-dir', git_dir, '--work-tree',
                juju_gui_source_dir, 'checkout', revision[1:]))
    else:
        log('Retrieving Juju GUI source from {} (branch: {}).'.format(
            branch_url, revision))
        cmd_log(
            git_clone('--depth', '1', '-b', revision, branch_url,
                      juju_gui_source_dir))

    log('Preparing a Juju GUI release.')
    logdir = os.path.dirname(logpath)

    fd, name = tempfile.mkstemp(prefix='make-distfile-', dir=logdir)
    log('Output from "make distfile" sent to %s' % name)

    # Passing HOME is required by node during npm packages installation.
    run('make',
        '-C',
        juju_gui_source_dir,
        'distfile',
        'BRANCH_IS_GOOD=true',
        'HOME={}'.format(os.path.expanduser('~')),
        stdout=fd,
        stderr=fd)

    return first_path_in_dir(os.path.join(juju_gui_source_dir, 'releases'))
コード例 #32
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def setup_apache_config(build_dir, serve_tests=False):
    """Set up the Apache configuration."""
    log('Generating the Apache site configuration files.')
    tests_root = os.path.join(JUJU_GUI_DIR, 'test', '') if serve_tests else ''
    context = {
        'port': WEB_PORT,
        'server_root': build_dir,
        'tests_root': tests_root,
    }
    render_to_file('apache-ports.template', context, APACHE_PORTS)
    cmd_log(run('chown', 'ubuntu:', APACHE_PORTS))
    render_to_file('apache-site.template', context, APACHE_SITE)
    cmd_log(run('chown', 'ubuntu:', APACHE_SITE))
    with su('root'):
        run('a2dissite', 'default')
        run('a2ensite', 'juju-gui')
        run('a2enmod', 'headers')
コード例 #33
0
ファイル: utils.py プロジェクト: bac/juju-gui-charm
def start_builtin_server(
        ssl_cert_path, serve_tests, sandbox, builtin_server_logging,
        insecure, charmworld_url, env_password=None, env_uuid=None,
        juju_version=None, debug=False, port=None, jem_location=None,
        interactive_login=False):
    """Start the builtin server."""
    if (port is not None) and not port_in_range(port):
        # Do not use the user provided port if it is not valid.
        port = None
    write_builtin_server_startup(
        ssl_cert_path, serve_tests=serve_tests, sandbox=sandbox,
        builtin_server_logging=builtin_server_logging, insecure=insecure,
        charmworld_url=charmworld_url, env_password=env_password,
        env_uuid=env_uuid, juju_version=juju_version,
        debug=debug, port=port, jem_location=jem_location,
        interactive_login=interactive_login)
    log('Starting the builtin server.')
    with su('root'):
        service_control(GUISERVER, RESTART)
コード例 #34
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def start_agent(ssl_cert_path, read_only=False):
    """Start the Juju agent and connect to the current environment."""
    # Retrieve the Zookeeper address from the start up script.
    unit_name = os.path.basename(
        os.path.realpath(os.path.join(CURRENT_DIR, '..')))
    agent_file = os.path.join(SYS_INIT_DIR, 'juju-{}.conf'.format(unit_name))
    zookeeper = get_zookeeper_address(agent_file)
    log('Setting up the API agent Upstart script.')
    context = {
        'juju_dir': JUJU_AGENT_DIR,
        'keys': ssl_cert_path,
        'port': API_PORT,
        'zookeeper': zookeeper,
        'read_only': read_only
    }
    render_to_file('juju-api-agent.conf.template', context, AGENT_INIT_PATH)
    log('Starting the API agent.')
    with su('root'):
        service_control(AGENT, START)
コード例 #35
0
ファイル: backend.py プロジェクト: CiscoSystems/jujucharm-n1k
 def start(self, backend):
     log("Starting Juju GUI.")
     config = backend.config
     build_dir = utils.compute_build_dir(config["juju-gui-debug"], config["serve-tests"])
     utils.write_gui_config(
         config["juju-gui-console-enabled"],
         config["login-help"],
         config["read-only"],
         config["staging"],
         config["charmworld-url"],
         build_dir,
         secure=config["secure"],
         sandbox=config["sandbox"],
         ga_key=config["ga-key"],
         default_viewmode=config["default-viewmode"],
         show_get_juju_button=config["show-get-juju-button"],
         password=config.get("password"),
     )
     # Expose the service.
     open_port(80)
     open_port(443)
コード例 #36
0
ファイル: backend.py プロジェクト: kadams54/juju-gui-charm
    def install(self, backend):
        """Install the GUI and dependencies."""
        # If the source setting has changed since the last time this was run,
        # get the code, from either a static release or a branch as specified
        # by the souce setting, and install it.
        if backend.different("juju-gui-source"):
            # Get a tarball somehow.
            origin, version_or_branch = utils.parse_source(backend.config["juju-gui-source"])
            if origin == "develop":
                # Develop is the latest passing build from Git.
                origin = "branch"
                version_or_branch = ("https://github.com/juju/juju-gui.git", "develop")

            if origin == "branch":
                logpath = backend.config["command-log-file"]
                # Make sure we have the required build dependencies.
                # Note that we also need to add the juju-gui repository
                # containing our version of nodejs.
                log("Installing build dependencies.")
                utils.install_missing_packages(
                    utils.DEB_BUILD_DEPENDENCIES, repository=backend.config["repository-location"]
                )

                branch_url, revision = version_or_branch
                log("Using source {}: {}".format(branch_url, revision))

                release_tarball_path = utils.fetch_gui_from_branch(branch_url, revision, logpath)
            else:
                release_tarball_path = utils.fetch_gui_release(origin, version_or_branch)
            # Install the tarball.
            utils.setup_gui(release_tarball_path)
        else:
            log("No change to juju-gui-source. Skipping step.")
コード例 #37
0
def fetch_gui_release(origin, version):
    """Retrieve a Juju GUI release. Return the release tarball local path.

    The release file can be retrieved from:
      - an arbitrary URL (if origin is "url");
      - the local releases repository (if origin is "local" or if a release
        version is specified and the corresponding file is present locally);
      - Launchpad (in all the other cases).
    """
    log('Retrieving Juju GUI release.')
    if origin == 'url':
        # "version" is a url.
        _, _, extension = version.rpartition('.')
        if extension not in ('tgz', 'xz'):
            extension = 'xz'
        return download_release(version, 'url-release.' + extension)
    if origin == 'local':
        path = get_release_file_path()
        log('Using a local release: {}'.format(path))
        return path
    # Handle "stable"
    if version is not None:
        # If the user specified a version, before attempting to download the
        # requested release from Launchpad, check if that version is already
        # stored locally.
        path = get_release_file_path(version)
        if path is not None:
            log('Using a local release: {}'.format(path))
            return path
    # Retrieve a release from Launchpad.
    launchpad = Launchpad.login_anonymously('Juju GUI charm', 'production')
    project = launchpad.projects['juju-gui']
    url, filename = get_launchpad_release(project, origin, version)
    return download_release(url, filename)
コード例 #38
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def fetch_gui_release(origin, version):
    """Retrieve a Juju GUI release. Return the release tarball local path.

    The release file can be retrieved from:
      - an arbitrary URL (if origin is "url");
      - the local releases repository (if origin is "local" or if a release
        version is specified and the corresponding file is present locally);
      - Launchpad (in all the other cases).
    """
    log('Retrieving Juju GUI release.')
    if origin == 'url':
        # "version" is a url.
        _, _, extension = version.rpartition('.')
        if extension not in ('tgz', 'xz'):
            extension = 'xz'
        return download_release(version, 'url-release.' + extension)
    if origin == 'local':
        path = get_release_file_path()
        log('Using a local release: {}'.format(path))
        return path
    # Handle "stable" and "trunk" origins.
    if version is not None:
        # If the user specified a version, before attempting to download the
        # requested release from Launchpad, check if that version is already
        # stored locally.
        path = get_release_file_path(version)
        if path is not None:
            log('Using a local release: {}'.format(path))
            return path
    # Retrieve a release from Launchpad.
    launchpad = Launchpad.login_anonymously('Juju GUI charm', 'production')
    project = launchpad.projects['juju-gui']
    url, filename = get_launchpad_release(project, origin, version)
    return download_release(url, filename)
コード例 #39
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def write_gui_config(
        console_enabled, login_help, readonly, in_staging, charmworld_url,
        build_dir, secure=True, sandbox=False,
        default_viewmode='sidebar', show_get_juju_button=False,
        config_js_path=None, ga_key='', password=None):
    """Generate the GUI configuration file."""
    log('Generating the Juju GUI configuration file.')
    is_legacy_juju = legacy_juju()
    user = '******' if is_legacy_juju else 'user-admin'
    # Normalize empty string passwords to None.
    password = password if password else None
    if password is None and ((is_legacy_juju and in_staging) or sandbox):
        password = '******'
    api_backend = 'python' if (is_legacy_juju and not sandbox) else 'go'
    if secure:
        protocol = 'wss'
    else:
        log('Running in insecure mode! Port 80 will serve unencrypted.')
        protocol = 'ws'
    context = {
        'raw_protocol': protocol,
        'address': unit_get('public-address'),
        'console_enabled': json.dumps(console_enabled),
        'login_help': json.dumps(login_help),
        'password': json.dumps(password),
        'api_backend': json.dumps(api_backend),
        'readonly': json.dumps(readonly),
        'user': json.dumps(user),
        'protocol': json.dumps(protocol),
        'sandbox': json.dumps(sandbox),
        'charmworld_url': json.dumps(charmworld_url),
        'ga_key': json.dumps(ga_key),
        'default_viewmode': json.dumps(default_viewmode),
        'show_get_juju_button': json.dumps(show_get_juju_button),
    }
    if config_js_path is None:
        config_js_path = os.path.join(
            build_dir, 'juju-ui', 'assets', 'config.js')
    render_to_file('config.js.template', context, config_js_path)
コード例 #40
0
def log_hook():
    """Log when a hook starts and stops its execution.

    Also log to stdout possible CalledProcessError exceptions raised executing
    the hook.
    """
    script = script_name()
    log(">>> Entering {}".format(script))
    try:
        yield
    except CalledProcessError as err:
        log('Exception caught:')
        log(err.output)
        raise
    finally:
        log("<<< Exiting {}".format(script))
コード例 #41
0
def start_builtin_server(build_dir,
                         ssl_cert_path,
                         serve_tests,
                         sandbox,
                         builtin_server_logging,
                         insecure,
                         charmworld_url,
                         port=None):
    """Start the builtin server."""
    if (port is not None) and not port_in_range(port):
        # Do not use the user provided port if it is not valid.
        port = None
    write_builtin_server_startup(build_dir,
                                 ssl_cert_path,
                                 serve_tests=serve_tests,
                                 sandbox=sandbox,
                                 builtin_server_logging=builtin_server_logging,
                                 insecure=insecure,
                                 charmworld_url=charmworld_url,
                                 port=port)
    log('Starting the builtin server.')
    with su('root'):
        service_control(GUISERVER, RESTART)
コード例 #42
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def log_hook():
    """Log when a hook starts and stops its execution.

    Also log to stdout possible CalledProcessError exceptions raised executing
    the hook.
    """
    script = script_name()
    log(">>> Entering {}".format(script))
    try:
        yield
    except CalledProcessError as err:
        log('Exception caught:')
        log(err.output)
        raise
    finally:
        log("<<< Exiting {}".format(script))
コード例 #43
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def install_missing_packages(packages, repository=None):
    """Install the required debian packages if they are missing.

    If repository is not None, add the given apt repository before installing
    the dependencies.
    """
    missing = find_missing_packages(*packages)
    if missing:
        if repository is not None:
            log('Adding the apt repository {}.'.format(repository))
            install_extra_repositories(repository)
        log('Installing deb packages: {}.'.format(', '.join(missing)))
        cmd_log(apt_get_install(*missing))
    else:
        log('No missing deb packages.')
コード例 #44
0
def install_missing_packages(packages, repository=None):
    """Install the required debian packages if they are missing.

    If repository is not None, add the given apt repository before installing
    the dependencies.
    """
    missing = find_missing_packages(*packages)
    if missing:
        if repository is not None:
            log('Adding the apt repository {}.'.format(repository))
            install_extra_repositories(repository)
        log('Installing deb packages: {}.'.format(', '.join(missing)))
        cmd_log(apt_get_install(*missing))
    else:
        log('No missing deb packages.')
コード例 #45
0
ファイル: utils.py プロジェクト: CiscoSystems/jujucharm-n1k
def fetch_gui_from_branch(branch_url, revision, logpath):
    """Retrieve the Juju GUI from a branch and build a release archive."""
    # Inject NPM packages into the cache for faster building.
    prime_npm_cache(get_npm_cache_archive_url())
    # Create a release starting from a branch.
    juju_gui_source_dir = os.path.join(CURRENT_DIR, 'juju-gui-source')
    checkout_args, revno = ([], 'latest revno') if revision is None else (
        ['--revision', revision], 'revno {}'.format(revision))
    log('Retrieving Juju GUI source checkout from {} ({}).'.format(
        branch_url, revno))
    cmd_log(run('rm', '-rf', juju_gui_source_dir))
    checkout_args.extend([branch_url, juju_gui_source_dir])
    cmd_log(bzr_checkout(*checkout_args))
    log('Preparing a Juju GUI release.')
    logdir = os.path.dirname(logpath)
    fd, name = tempfile.mkstemp(prefix='make-distfile-', dir=logdir)
    log('Output from "make distfile" sent to %s' % name)
    with environ(NO_BZR='1'):
        run('make', '-C', juju_gui_source_dir, 'distfile',
            stdout=fd, stderr=fd)
    return first_path_in_dir(
        os.path.join(juju_gui_source_dir, 'releases'))
コード例 #46
0
    def install(self, backend):
        """Install the GUI and dependencies."""
        # If the source setting has changed since the last time this was run,
        # get the code, from either a static release or a branch as specified
        # by the souce setting, and install it.
        if backend.different('juju-gui-source'):
            # Get a tarball somehow.
            origin, version_or_branch = utils.parse_source(
                backend.config['juju-gui-source'])
            if origin == 'develop':
                # Develop is the latest passing build from Git.
                origin = 'branch'
                version_or_branch = ('https://github.com/juju/juju-gui.git',
                                     'develop')

            if origin == 'branch':
                logpath = backend.config['command-log-file']
                # Make sure we have the required build dependencies.
                # Note that we also need to add the juju-gui repository
                # containing our version of nodejs.
                log('Installing build dependencies.')
                utils.install_missing_packages(
                    utils.DEB_BUILD_DEPENDENCIES,
                    repository=backend.config['repository-location'])

                branch_url, revision = version_or_branch
                log('Using source {}: {}'.format(branch_url, revision))

                release_tarball_path = utils.fetch_gui_from_branch(
                    branch_url, revision, logpath)
            else:
                release_tarball_path = utils.fetch_gui_release(
                    origin, version_or_branch)
            # Install the tarball.
            utils.setup_gui(release_tarball_path)
        else:
            log('No change to juju-gui-source. Skipping step.')
コード例 #47
0
 def install(self):
     """Execute the installation steps."""
     log('Installing dependencies.')
     utils.install_missing_packages(self.get_dependencies())
     call_methods(self.mixins, 'install', self)
コード例 #48
0
 def destroy(self, backend):
     log('Cleaning up base dir: {}.'.format(utils.BASE_DIR))
     shutil.rmtree(utils.BASE_DIR)
コード例 #49
0
def write_gui_config(console_enabled,
                     login_help,
                     readonly,
                     charmworld_url,
                     charmstore_url,
                     build_dir,
                     secure=True,
                     sandbox=False,
                     cached_fonts=False,
                     hide_login_button=False,
                     config_js_path=None,
                     ga_key='',
                     juju_core_version=None,
                     password=None,
                     juju_env_uuid=None):
    """Generate the GUI configuration file."""
    log('Generating the Juju GUI configuration file.')
    user = '******'
    # Normalize empty string passwords to None. If sandbox is enabled then set
    # the password to admin and it will auto login.
    if not password:
        if sandbox:
            password = '******'
        else:
            password = None
    api_backend = 'go'
    if secure:
        protocol = 'wss'
    else:
        log('Running in insecure mode! Port 80 will serve unencrypted.')
        protocol = 'ws'
    # Set up the help message displayed by the GUI login view.
    if login_help is None:
        env_name = os.getenv('JUJU_ENV_NAME')
        if env_name:
            login_help = (
                'The password is the admin-secret from the Juju environment. '
                'This can be found by looking in ~/.juju/environments/{}.jenv '
                'and searching for the password field. Note that using '
                'juju-quickstart (https://launchpad.net/juju-quickstart) can '
                'automate logging in, as well as other parts of installing '
                'and starting Juju.'.format(env_name))
        else:
            # The Juju environment name is included in the hooks context
            # starting from juju-core v1.18.
            login_help = (
                'The password for newer Juju clients can be found by locating '
                'the Juju environment file placed in ~/.juju/environments/ '
                'with the same name as the current environment. For example, '
                'if you have an environment named "production", then the file '
                'is named ~/.juju/environments/production.jenv. Look for the '
                '"password" field in the file, or if that is empty, for the '
                '"admin-secret". Remove the quotes from the value, and use '
                'this to log in. The password for older Juju clients (< 1.16) '
                'is in ~/.juju/environments.yaml, and listed as the '
                'admin-secret for the environment you are using. Note that '
                'using juju-quickstart '
                '(https://launchpad.net/juju-quickstart) can automate logging '
                'in, as well as other parts of installing and starting Juju.')
    if not juju_core_version:
        log('Retrieving Juju version.')
        juju_core_version = run('jujud', '--version').strip()

    context = {
        'cached_fonts': json.dumps(cached_fonts),
        'raw_protocol': protocol,
        'address': unit_get('public-address'),
        'console_enabled': json.dumps(console_enabled),
        'login_help': json.dumps(login_help),
        'password': json.dumps(password),
        'api_backend': json.dumps(api_backend),
        'readonly': json.dumps(readonly),
        'user': json.dumps(user),
        'protocol': json.dumps(protocol),
        'sandbox': json.dumps(sandbox),
        'charmworld_url': json.dumps(charmworld_url),
        'charmstore_url': json.dumps(charmstore_url),
        'ga_key': json.dumps(ga_key),
        'hide_login_button': json.dumps(hide_login_button),
        'juju_core_version': json.dumps(juju_core_version),
        'juju_env_uuid': json.dumps(juju_env_uuid),
    }
    if config_js_path is None:
        config_js_path = os.path.join(build_dir, 'juju-ui', 'assets',
                                      'config.js')
    render_to_file('config.js.template', context, config_js_path)
コード例 #50
0
def stop_builtin_server():
    """Stop the builtin server."""
    log('Stopping the builtin server.')
    with su('root'):
        service_control(GUISERVER, STOP)
    cmd_log(run('rm', '-f', GUISERVER_INIT_PATH))