~fginther/charms/trusty/jenkaas/add-default-user

« back to all changes in this revision

Viewing changes to hooks/actions.py

  • Committer: Francis Ginther
  • Date: 2015-06-19 02:59:34 UTC
  • Revision ID: francis.ginther@canonical.com-20150619025934-xsgeqrfe2e1k0lbg
Add support for default security and a default jenkins user.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import glob
2
2
import grp
 
3
import hashlib
3
4
import os
4
5
import pwd
5
6
import shutil
6
7
import subprocess
7
8
 
8
9
from charmhelpers import fetch
9
 
from charmhelpers.core import hookenv
10
 
from charmhelpers.core.host import mkdir
 
10
from charmhelpers.core import (hookenv, host)
11
11
from charmhelpers.payload import execd
12
12
 
13
13
SERVICE_NAME = 'jenkaas'
14
14
SERVICE_CONFIGNAME = 'jenkaas.conf'
15
 
DEPS_PKGES = ["daemon", "adduser", "psmisc", "default-jre"]
 
15
DEPS_PKGES = ["daemon", "adduser", "psmisc", "default-jre", "pwgen"]
16
16
 
17
17
config = hookenv.config()
18
18
 
41
41
    plugins = glob.glob(os.path.join(charm_plugins_dir, '*.hpi'))
42
42
    service_plugin_dir = os.path.join(_service_dir(), 'plugins')
43
43
    if not os.path.exists(service_plugin_dir):
44
 
        mkdir(service_plugin_dir, 'jenkins', 'jenkins', 0o755)
 
44
        host.mkdir(service_plugin_dir, 'jenkins', 'jenkins', 0o755)
45
45
    uid = pwd.getpwnam("jenkins").pw_uid
46
46
    gid = grp.getgrnam("jenkins").gr_gid
47
47
    for plugin in plugins:
56
56
    fetch.apt_install(DEPS_PKGES, options=['--fix-broken', ], fatal=True)
57
57
 
58
58
 
 
59
def install_jenkins_config(service_name):
 
60
    hookenv.log('Installing jenkins config')
 
61
    in_config = os.path.join(hookenv.charm_dir(),
 
62
                             'files/templates/config.xml')
 
63
    shutil.copy(in_config, _service_dir())
 
64
 
 
65
 
 
66
def configure_default_user(service_name):
 
67
    hookenv.log('Configuring default user')
 
68
    username = config['username']
 
69
    password = config['password']
 
70
    salt = subprocess.check_output(['pwgen', '-N1', '6']).strip()
 
71
    csum = hashlib.sha256('{}{{{}}}'.format(password, salt)).hexdigest()
 
72
    salty_password = '{}:{}'.format(salt, csum)
 
73
 
 
74
    users_path = os.path.join(_service_dir(), 'users', username)
 
75
    if not os.path.exists(users_path):
 
76
        host.mkdir(users_path, 'jenkins', 'jenkins', 0o755)
 
77
 
 
78
    in_config = os.path.join(hookenv.charm_dir(),
 
79
                             'files/templates/user/config.xml')
 
80
    out_config = os.path.join(users_path, 'config.xml')
 
81
    with open(in_config) as in_file, open(out_config, 'w') as out_file:
 
82
        for line in in_file:
 
83
            if '<fullName>' in line:
 
84
                line = '  <fullName>{}</fullName>\n'.format(username)
 
85
            if '<passwordHash>' in line:
 
86
                line = '      <passwordHash>{}</passwordHash>\n'.format(
 
87
                    salty_password)
 
88
            out_file.write(line)
 
89
    host.chownr(out_config, 'jenkins', 'jenkins')
 
90
    os.chmod(out_config, 0o644)
 
91
 
 
92
 
59
93
def install_slaves(service_name):
60
94
    hookenv.log('Installing slaves')
61
95
    hookenv.log(hookenv.relations())