~mbruzek/charms/trusty/kubernetes-master/github-sync

« back to all changes in this revision

Viewing changes to hooks/kubernetes_installer.py

  • Committer: Charles Butler
  • Date: 2015-02-18 22:58:19 UTC
  • Revision ID: charles.butler@ubuntu.com-20150218225819-tei5yvbs7evb6b3l
Updates to: Readme instructions
Alters hooks to correct a transient error in symlinking
Updates hooks with path.py goodness

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
1
import os
4
 
import shutil
5
2
import subprocess
 
3
from path import path
6
4
 
7
5
 
8
6
class KubernetesInstaller():
20
18
                        'kubectl': 'kubectl'}
21
19
        self.arch = arch
22
20
        self.version = version
23
 
        self.kubernetes_file = kubernetes_file
 
21
        self.kubernetes_file = path(kubernetes_file)
24
22
 
25
 
    def install(self, output_dir='/opt/kubernetes/bin'):
 
23
    def install(self, output_dir=path('/opt/kubernetes')):
26
24
        """ Install kubernetes binary files from the tar file or gsutil. """
27
 
        if os.path.isdir(output_dir):
28
 
            # Remote old content to remain idempotent.
29
 
            shutil.rmtree(output_dir)
30
 
        # Create the output directory.
31
 
        os.makedirs(output_dir)
32
 
 
33
 
        if os.path.exists(self.kubernetes_file):
 
25
        bindir = output_dir / 'bin'
 
26
        if bindir.exists():
 
27
            for fp in bindir.files():
 
28
                fp.remove()
 
29
 
 
30
        bindir.makedirs_p()
 
31
 
 
32
        kubernetes_repo = 'http://github.com/GoogleCloudPlatform/kubernetes'
 
33
        repo_dir = output_dir / 'kubernetes/src'
 
34
        if not repo_dir.exists():
 
35
            git_clone = 'git clone {0} {1}'.format(kubernetes_repo, repo_dir)
 
36
            print(git_clone)
 
37
            subprocess.check_call(git_clone.split())
 
38
 
 
39
        if self.kubernetes_file.exists():
34
40
            # Untar the file to the output directory.
35
41
            command = 'tar -xvzf {0} -C {1}'.format(self.kubernetes_file,
36
 
                                                    output_dir)
 
42
                                                    bindir)
37
43
            print(command)
38
44
            output = subprocess.check_output(command, shell=True)
39
45
            print(output)
40
46
        else:
41
47
            # Get the binaries from the gsutil command.
42
 
            self.get_kubernetes_gsutil(output_dir)
 
48
            self.get_kubernetes_gsutil(bindir)
43
49
 
44
50
        # Create the symbolic links to the real kubernetes binaries.
45
51
        # This can be removed if the code is changed to call real commands.
46
 
        usr_local_bin = '/usr/local/bin'
 
52
        usr_local_bin = path('/usr/local/bin')
47
53
        for key, value in self.aliases.iteritems():
48
 
            target = os.path.join(output_dir, key)
49
 
            link = os.path.join(usr_local_bin, value)
50
 
            ln_command = 'ln -s {0} {1}'.format(target, link)
51
 
            command = ln_command.format(target, link)
52
 
            print(command)
53
 
            subprocess.check_call(command.split())
 
54
            target = bindir / key
 
55
            link = usr_local_bin / value
 
56
            if link.exists():
 
57
                link.remove()
 
58
            target.symlink(link)
54
59
 
55
60
    def get_kubernetes_gsutil(self, directory):
56
61
        """ Download the kubernetes binary objects from gsutil. """