Commit 3e118a4cf3765a39f17f2c1e2cb78b24d04dd349

Authored by Sergio Oliveira
2 parents fd68bc4f 24b89caf

Merge branch 'update_puppet' of github.com:colab-community/colab into update_puppet

Showing 2 changed files with 101 additions and 54 deletions   Show diff stats
1 # -*- mode: ruby -*- 1 # -*- mode: ruby -*-
2 # vi: set ft=ruby : 2 # vi: set ft=ruby :
3 3
  4 +# CHOOSE THE DISTRO FOR COLAB VM (set the distro variable):
  5 +# - ubuntu_precise
  6 +# - ubuntu_trusty
  7 +# - centos_65
  8 +
  9 +distro = "centos_65"
  10 +
4 # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! 11 # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
5 VAGRANTFILE_API_VERSION = "2" 12 VAGRANTFILE_API_VERSION = "2"
6 13
@@ -10,24 +17,43 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| @@ -10,24 +17,43 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
10 # please see the online documentation at vagrantup.com. 17 # please see the online documentation at vagrantup.com.
11 18
12 # Every Vagrant virtual environment requires a box to build off of. 19 # Every Vagrant virtual environment requires a box to build off of.
13 - config.vm.box = "trusty64"  
14 20
15 - # The url from where the 'config.vm.box' box will be fetched if it  
16 - # doesn't already exist on the user's system.  
17 - config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box" 21 + config.vm.define "colab" do |colab|
  22 +
  23 + colab.vm.box = distro
  24 +
  25 + if distro == "ubuntu_precise"
  26 + puts "Installing Ubuntu Precise VM!"
  27 + colab.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box"
  28 + elsif distro == "ubuntu_trusty"
  29 + puts "Installing Ubuntu Trusty VM!"
  30 + colab.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
  31 + elsif distro == "centos_65"
  32 + puts "Installing CentOS 6.5 VM!"
  33 + colab.vm.box_url = "https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box"
  34 + end
  35 +
  36 + colab.vm.network "forwarded_port", guest: 80, host: 80
  37 + colab.vm.network "private_network", ip: "192.168.33.10"
  38 + colab.vm.provider "virtualbox" do |vb|
  39 + # Use VBoxManage to customize the VM. For example to change memory:
  40 + vb.customize ["modifyvm", :id, "--memory", "1024"]
  41 + end
  42 + end
  43 +
  44 + # Disable automatic box update checking. If you disable this, then
  45 + # boxes will only be checked for updates when the user runs
  46 + # `vagrant box outdated`. This is not recommended.
  47 + # config.vm.box_check_update = false
18 48
19 # Create a forwarded port mapping which allows access to a specific port 49 # Create a forwarded port mapping which allows access to a specific port
20 # within the machine from a port on the host machine. In the example below, 50 # within the machine from a port on the host machine. In the example below,
21 # accessing "localhost:8080" will access port 80 on the guest machine. 51 # accessing "localhost:8080" will access port 80 on the guest machine.
22 - config.vm.network :forwarded_port, guest: 80, host: 8080  
23 - config.vm.network :forwarded_port, guest: 8000, host: 8000  
24 - config.vm.network :forwarded_port, guest: 5280, host: 5280  
25 - config.vm.network :forwarded_port, guest: 8080, host: 8081  
26 - config.vm.network :forwarded_port, guest: 8983, host: 8983 52 + # config.vm.network "forwarded_port", guest: 80, host: 8080
27 53
28 # Create a private network, which allows host-only access to the machine 54 # Create a private network, which allows host-only access to the machine
29 # using a specific IP. 55 # using a specific IP.
30 - config.vm.network "private_network", ip: "192.168.33.10" 56 + # config.vm.network "private_network", ip: "192.168.33.10"
31 57
32 # Create a public network, which generally matched to bridged network. 58 # Create a public network, which generally matched to bridged network.
33 # Bridged networks make the machine appear as another physical device on 59 # Bridged networks make the machine appear as another physical device on
@@ -43,40 +69,42 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| @@ -43,40 +69,42 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
43 # the path on the guest to mount the folder. And the optional third 69 # the path on the guest to mount the folder. And the optional third
44 # argument is a set of non-required options. 70 # argument is a set of non-required options.
45 # config.vm.synced_folder "../data", "/vagrant_data" 71 # config.vm.synced_folder "../data", "/vagrant_data"
46 - config.vm.synced_folder ".", "/vagrant", type: "nfs"  
47 72
48 # Provider-specific configuration so you can fine-tune various 73 # Provider-specific configuration so you can fine-tune various
49 # backing providers for Vagrant. These expose provider-specific options. 74 # backing providers for Vagrant. These expose provider-specific options.
50 # Example for VirtualBox: 75 # Example for VirtualBox:
51 # 76 #
52 - config.vm.provider "virtualbox" do |vb| 77 + # config.vm.provider "virtualbox" do |vb|
53 # # Don't boot with headless mode 78 # # Don't boot with headless mode
54 # vb.gui = true 79 # vb.gui = true
55 # 80 #
56 # # Use VBoxManage to customize the VM. For example to change memory: 81 # # Use VBoxManage to customize the VM. For example to change memory:
57 - vb.customize ["modifyvm", :id, "--memory", "1024"]  
58 - end 82 + # vb.customize ["modifyvm", :id, "--memory", "1024"]
  83 + # end
59 # 84 #
60 # View the documentation for the provider you're using for more 85 # View the documentation for the provider you're using for more
61 # information on available options. 86 # information on available options.
62 87
  88 + # Enable provisioning with CFEngine. CFEngine Community packages are
  89 + # automatically installed. For example, configure the host as a
  90 + # policy server and optionally a policy file to run:
  91 + #
  92 + # config.vm.provision "cfengine" do |cf|
  93 + # cf.am_policy_hub = true
  94 + # # cf.run_file = "motd.cf"
  95 + # end
  96 + #
  97 + # You can also configure and bootstrap a client to an existing
  98 + # policy server:
  99 + #
  100 + # config.vm.provision "cfengine" do |cf|
  101 + # cf.policy_server_address = "10.0.2.15"
  102 + # end
  103 +
63 # Enable provisioning with Puppet stand alone. Puppet manifests 104 # Enable provisioning with Puppet stand alone. Puppet manifests
64 # are contained in a directory path relative to this Vagrantfile. 105 # are contained in a directory path relative to this Vagrantfile.
65 # You will need to create the manifests directory and a manifest in 106 # You will need to create the manifests directory and a manifest in
66 - # the file base.pp in the manifests_path directory.  
67 - #  
68 - # An example Puppet manifest to provision the message of the day:  
69 - #  
70 - # # group { "puppet":  
71 - # # ensure => "present",  
72 - # # }  
73 - # #  
74 - # # File { owner => 0, group => 0, mode => 0644 }  
75 - # #  
76 - # # file { '/etc/motd':  
77 - # # content => "Welcome to your Vagrant-built virtual machine!  
78 - # # Managed by Puppet.\n"  
79 - # # } 107 + # the file default.pp in the manifests_path directory.
80 # 108 #
81 # config.vm.provision "puppet" do |puppet| 109 # config.vm.provision "puppet" do |puppet|
82 # puppet.manifests_path = "manifests" 110 # puppet.manifests_path = "manifests"
@@ -95,7 +123,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| @@ -95,7 +123,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
95 # chef.add_role "web" 123 # chef.add_role "web"
96 # 124 #
97 # # You may also specify custom JSON attributes: 125 # # You may also specify custom JSON attributes:
98 - # chef.json = { :mysql_password => "foo" } 126 + # chef.json = { mysql_password: "foo" }
99 # end 127 # end
100 128
101 # Enable provisioning with chef server, specifying the chef server URL, 129 # Enable provisioning with chef server, specifying the chef server URL,
@@ -9,6 +9,19 @@ from fabric.api import env, run, sudo, local @@ -9,6 +9,19 @@ from fabric.api import env, run, sudo, local
9 from fabric.contrib.files import exists 9 from fabric.contrib.files import exists
10 from fabric.context_managers import prefix, cd, settings, shell_env 10 from fabric.context_managers import prefix, cd, settings, shell_env
11 11
  12 +LINUX_DISTRO = ''
  13 +DEBIAN_FAMILY = ['debian', 'ubuntu']
  14 +REDHAT_FAMILY = ['centos', 'fedora']
  15 +
  16 +DISTRO_CMD = {'debian': ('apt-get', {
  17 + 'install': '-y',
  18 + 'update': '-y',
  19 + }),
  20 + 'redhat': ('yum', {
  21 + 'install': '-y',
  22 + 'update': '-y',
  23 + })
  24 + }
12 25
13 APP_USER = APP_NAME = VENV_NAME = 'colab' 26 APP_USER = APP_NAME = VENV_NAME = 'colab'
14 REPO_URL = 'git@github.com:colab-community/colab.git' 27 REPO_URL = 'git@github.com:colab-community/colab.git'
@@ -34,6 +47,19 @@ WORKON_ENV = '{} && workon {}'.format(SOURCE_VENV, VENV_NAME) @@ -34,6 +47,19 @@ WORKON_ENV = '{} && workon {}'.format(SOURCE_VENV, VENV_NAME)
34 MANAGE_PATH = os.path.join(REPO_PATH, 'src') 47 MANAGE_PATH = os.path.join(REPO_PATH, 'src')
35 SETTINGS_PATH = os.path.join(MANAGE_PATH, APP_NAME) 48 SETTINGS_PATH = os.path.join(MANAGE_PATH, APP_NAME)
36 49
  50 +def get_distro_family():
  51 + linux_name = run('python -c "import platform; print platform.dist()[0]"').lower()
  52 + if linux_name in DEBIAN_FAMILY:
  53 + return 'debian'
  54 + elif linux_name in REDHAT_FAMILY:
  55 + return 'redhat'
  56 + else :
  57 + error(colors.red('Distribuiton `{}` not supported.'.format(linux_name)))
  58 + exit(1)
  59 +
  60 +def cmd(family, command, args = ''):
  61 + pkgmanager, commands = DISTRO_CMD[family]
  62 + return '{} {} {} {}'.format(pkgmanager, command, commands[command], args)
37 63
38 @task 64 @task
39 def environment(name=DEFAULT_ENVIRONMENT): 65 def environment(name=DEFAULT_ENVIRONMENT):
@@ -53,12 +79,10 @@ def environment(name=DEFAULT_ENVIRONMENT): @@ -53,12 +79,10 @@ def environment(name=DEFAULT_ENVIRONMENT):
53 79
54 env.update(environments[name]) 80 env.update(environments[name])
55 env.environment = name 81 env.environment = name
56 -environment()  
57 -  
58 -  
59 -def aptget_install(pkg):  
60 - sudo('DEBIAN_FRONTEND=noninteractive apt-get install -y -q {}'.format(pkg))  
61 82
  83 +def package_install(pkg):
  84 + family = get_distro_family()
  85 + sudo(cmd(family, 'install', pkg ))
62 86
63 def install_requirements(): 87 def install_requirements():
64 with cd(REPO_PATH), prefix(WORKON_ENV): 88 with cd(REPO_PATH), prefix(WORKON_ENV):
@@ -72,14 +96,12 @@ def install_requirements(): @@ -72,14 +96,12 @@ def install_requirements():
72 else: 96 else:
73 run('pip install -r requirements.txt') 97 run('pip install -r requirements.txt')
74 98
75 -  
76 def mkvirtualenv(): 99 def mkvirtualenv():
77 if not exists('~/.virtualenvs/' + VENV_NAME): 100 if not exists('~/.virtualenvs/' + VENV_NAME):
78 with prefix(SOURCE_VENV): 101 with prefix(SOURCE_VENV):
79 run('mkvirtualenv ' + VENV_NAME) 102 run('mkvirtualenv ' + VENV_NAME)
80 return True 103 return True
81 104
82 -  
83 def manage(command): 105 def manage(command):
84 django_settings = env.get('django_settings') 106 django_settings = env.get('django_settings')
85 env_vars = {} 107 env_vars = {}
@@ -90,21 +112,17 @@ def manage(command): @@ -90,21 +112,17 @@ def manage(command):
90 with cd(MANAGE_PATH), prefix(WORKON_ENV): 112 with cd(MANAGE_PATH), prefix(WORKON_ENV):
91 run('python manage.py {}'.format(command)) 113 run('python manage.py {}'.format(command))
92 114
93 -  
94 def syncdb(): 115 def syncdb():
95 manage('syncdb') 116 manage('syncdb')
96 117
97 -  
98 def migrate(): 118 def migrate():
99 manage('migrate') 119 manage('migrate')
100 120
101 -  
102 def collectstatic(): 121 def collectstatic():
103 sudo('mkdir -p /usr/share/nginx/{}'.format(APP_NAME)) 122 sudo('mkdir -p /usr/share/nginx/{}'.format(APP_NAME))
104 sudo('chown {} /usr/share/nginx/{}'.format(env.user, APP_NAME)) 123 sudo('chown {} /usr/share/nginx/{}'.format(env.user, APP_NAME))
105 manage('collectstatic --noinput') 124 manage('collectstatic --noinput')
106 125
107 -  
108 def create_local_settings(): 126 def create_local_settings():
109 with cd(SETTINGS_PATH), settings(user=env.superuser): 127 with cd(SETTINGS_PATH), settings(user=env.superuser):
110 env_local_settings = 'local_settings-{}.py'.format(env.environment) 128 env_local_settings = 'local_settings-{}.py'.format(env.environment)
@@ -113,7 +131,6 @@ def create_local_settings(): @@ -113,7 +131,6 @@ def create_local_settings():
113 run('ln -s {} {}'.format(env_local_settings, 'local_settings.py')) 131 run('ln -s {} {}'.format(env_local_settings, 'local_settings.py'))
114 run('chown {} local_settings.py'.format(env.user)) 132 run('chown {} local_settings.py'.format(env.user))
115 133
116 -  
117 def update_code(): 134 def update_code():
118 if env.is_vagrant: 135 if env.is_vagrant:
119 if not exists(REPO_PATH): 136 if not exists(REPO_PATH):
@@ -126,24 +143,27 @@ def update_code(): @@ -126,24 +143,27 @@ def update_code():
126 with cd(REPO_PATH): 143 with cd(REPO_PATH):
127 run('git pull') 144 run('git pull')
128 145
129 -  
130 @task 146 @task
131 def bootstrap(): 147 def bootstrap():
132 """Bootstrap machine to run fabric tasks""" 148 """Bootstrap machine to run fabric tasks"""
133 -  
134 with settings(user=env.superuser): 149 with settings(user=env.superuser):
135 -  
136 - if not exists('/usr/bin/git'):  
137 - aptget_install('git') 150 + family = get_distro_family()
  151 + sudo(cmd(family, 'update'))
  152 +
  153 + if not exists('/usr/bin/git'):
  154 + package_install('git-core')
138 155
139 if env.is_vagrant: 156 if env.is_vagrant:
140 - groups = 'sudo,vagrant' 157 + groups = ['sudo' ,'vagrant']
141 local('chmod -fR g+w {}'.format(PROJECT_PATH)) 158 local('chmod -fR g+w {}'.format(PROJECT_PATH))
142 else: 159 else:
143 - groups = 'sudo' 160 + groups = ['sudo']
  161 +
  162 + for group in groups:
  163 + sudo('groupadd -f {}'.format(group))
  164 +
  165 + sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, ','.join(groups)))
144 166
145 - sudo('useradd {} -G {} -m -s /bin/bash'.format(APP_USER, groups),  
146 - quiet=True)  
147 ssh_dir = '/home/{0}/.ssh/'.format(APP_USER) 167 ssh_dir = '/home/{0}/.ssh/'.format(APP_USER)
148 if not exists(ssh_dir): 168 if not exists(ssh_dir):
149 sudo('mkdir -p {0}'.format(ssh_dir)) 169 sudo('mkdir -p {0}'.format(ssh_dir))
@@ -162,13 +182,11 @@ def bootstrap(): @@ -162,13 +182,11 @@ def bootstrap():
162 sudo('chmod 440 {}'.format(tmp_file)) 182 sudo('chmod 440 {}'.format(tmp_file))
163 sudo('mv {} {}'.format(tmp_file, sudoers_file)) 183 sudo('mv {} {}'.format(tmp_file, sudoers_file))
164 184
165 -  
166 @task 185 @task
167 def provision(): 186 def provision():
168 """Run puppet""" 187 """Run puppet"""
169 188
170 update_code() 189 update_code()
171 -  
172 puppet_path = os.path.join(REPO_PATH, 'puppet/') 190 puppet_path = os.path.join(REPO_PATH, 'puppet/')
173 modules_path = os.path.join(puppet_path, 'modules') 191 modules_path = os.path.join(puppet_path, 'modules')
174 puppet_modules = '{}:/etc/puppet/modules'.format(modules_path) 192 puppet_modules = '{}:/etc/puppet/modules'.format(modules_path)
@@ -187,7 +205,6 @@ def provision(): @@ -187,7 +205,6 @@ def provision():
187 205
188 sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd)) 206 sudo('puppet apply --modulepath={} {}'.format(puppet_modules, cmd))
189 207
190 -  
191 @task 208 @task
192 def ssh_keygen(): 209 def ssh_keygen():
193 """Create SSH credentials""" 210 """Create SSH credentials"""
@@ -201,7 +218,6 @@ def ssh_keygen(): @@ -201,7 +218,6 @@ def ssh_keygen():
201 print('') 218 print('')
202 print('Add the key above to your github repository deploy keys') 219 print('Add the key above to your github repository deploy keys')
203 220
204 -  
205 @task 221 @task
206 def deploy(noprovision=False): 222 def deploy(noprovision=False):
207 """Deploy and run the new code (master branch)""" 223 """Deploy and run the new code (master branch)"""
@@ -222,3 +238,6 @@ def deploy(noprovision=False): @@ -222,3 +238,6 @@ def deploy(noprovision=False):
222 migrate() 238 migrate()
223 239
224 sudo('supervisorctl start all') 240 sudo('supervisorctl start all')
  241 +
  242 +# Main
  243 +environment()