702094218dbcd20c3357ecc8344018033fcfeea7
[obnox/vagrant/vagrant-samba-ad-dc.git] / Vagrantfile
1 # -*- mode: ruby -*-
2 # vi: set ft=ruby :
3
4 ENV['VAGRANT_DEFAULT_PROVIDER'] = 'lxc'
5
6 VAGRANTFILE_API_VERSION = 2
7
8
9 require 'yaml'
10
11 #
12 # Defaults for Configuration data.
13 # Will be overridden from the settings file
14 # and (possibly later) from commandline parameters.
15 #
16
17 net_default = {
18   :type   => 'veth',
19   :flags  => 'up',
20   :hwaddr => '',
21   :name   => '',
22   :ipv4   => '',
23   :ipv6   => '',
24 }
25
26 network_opts = [ :type, :link, :flags, :hwaddr, :name, :ipv4, :ipv6 ]
27
28 vms = [
29   {
30     :hostname => 's4dc-180',
31     :box => 'fgrehm/trusty64-lxc',
32     :provider => {
33       :lxc => {
34         :container_name => 's4dc-180',
35       },
36     },
37     :networks => [
38       {
39         :link => 'virbr1',
40         :ipv4 => '172.16.1.180',
41       },
42       #{
43       #  :link => 'virbr2',
44       #  #:ipv4 => '10.111.222.201',
45       #},
46     ],
47     :role => 's4dc',
48     :config => {
49       :s4dc => {
50         :domain => 'S4AD',
51         :realm => 's4ad.private',
52         :adminpass => 'Passw0rd',
53         :function_level => '2008_R2',
54       },
55     },
56   },
57 ]
58
59 #
60 # Load the config, if it exists,
61 # possibly override with commandline args,
62 # (currently none supported yet)
63 # and then store the config.
64 #
65
66 projectdir = File.expand_path File.dirname(__FILE__)
67 f = File.join(projectdir, 'vagrant.yaml')
68 if File.exists?(f)
69   settings = YAML::load_file f
70
71   if settings[:vms].is_a?(Array)
72     vms = settings[:vms]
73   end
74   puts "Loaded settings from #{f}."
75 end
76
77 # TODO(?): ARGV-processing
78
79 settings = {
80   :vms  => vms,
81 }
82
83 File.open(f, 'w') do |file|
84   file.write settings.to_yaml
85 end
86 puts "Wrote settings to #{f}."
87
88 # apply net defaults:
89
90 vms.each do |vm|
91   vm[:networks].each do |net|
92     net_default.keys.each do |key|
93       if not net.has_key?(key)
94         net[key] = net_default[key]
95       end
96     end
97   end
98 end
99
100
101 SCRIPT_INSTALL = <<SCRIPT
102 set -e
103
104 #apt-get -y update
105 #apt-get -y upgrade
106 apt-get -y install samba dnsutils
107 SCRIPT
108
109 SCRIPT_PROVISION = <<SCRIPT
110 set -e
111
112 DOMAIN=$1
113 REALM=$2
114 ADMINPASS=$3
115 FUNCTION_LEVEL=$4
116
117 stop smbd || true
118 stop nmbd || true
119 stop samba-ad-dc || true
120
121 BACKUP_SUFFIX=".orig.$(date +%Y%m%d-%H%M%S)"
122
123 FILE=/etc/samba/smb.conf
124 test -f ${FILE} || touch ${FILE}
125 mv -f ${FILE} ${FILE}${BACKUP_SUFFIX}
126
127 samba-tool domain provision \
128         --domain=${DOMAIN} \
129         --realm=${REALM} \
130         --adminpass=${ADMINPASS} \
131         --server-role=dc \
132         --function-level=${FUNCTION_LEVEL} \
133         --use-rfc2307
134
135 FILE=/etc/resolv.conf
136 test -f ${FILE} || touch ${FILE}
137 mv -f ${FILE} ${FILE}${BACKUP_SUFFIX}
138 cat <<EOF > ${FILE}
139 nameserver 127.0.0.1
140 domain ${REALM}
141 search ${REALM}
142 EOF
143
144 sudo sed -i${BACKUP_SUFFIX} -e 's/passwd: .*/passwd:         files winbind/g' -e 's/group: .*/group:          files winbind/g' /etc/nsswitch.conf
145
146 start samba-ad-dc
147 SCRIPT
148
149
150 Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
151
152   if Vagrant.has_plugin?("vagrant-cachier")
153     config.cache.scope = :box
154   end
155
156   vms.each do |machine|
157     config.vm.define machine[:hostname] do |node|
158       node.vm.box = machine[:box]
159       node.vm.hostname = machine[:hostname]
160       node.vm.provider :lxc do |lxc|
161         lxc.container_name = machine[:provider][:lxc][:container_name]
162
163         machine[:networks].each do |net|
164           network_opts.each do |key|
165             if not net[key] == ''
166              lxc.customize "network.#{key}", net[key]
167             end
168           end
169         end
170       end
171
172       #node.vm.synced_folder "shared/", "/shared"
173
174       node.vm.provision :shell, inline: SCRIPT_INSTALL
175       if machine[:role] == 's4dc'
176         cfg = machine[:config][:s4dc]
177         node.vm.provision :shell do |s|
178           s.inline = SCRIPT_PROVISION
179           s.args   = [ cfg[:domain], cfg[:realm], cfg[:adminpass],
180                        cfg[:function_level] ]
181         end
182       end
183     end
184   end
185
186   #
187   # Provisioning common to all machines:
188   #
189
190 end