This repository has been archived by the owner on Feb 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Vagrantfile
278 lines (238 loc) · 9.31 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# -*- mode: ruby -*-
# vi: set ft=ruby :
## Need to install dotenv in your vagrant environment
## vagrant plugin install vagrant-dotenv
REQUIRED_PLUGINS = %w(dotenv deep_merge)
REQUIRED_PLUGINS.each do |plugin|
unless Vagrant.has_plugin?(plugin) || ARGV[0] == 'plugin' then
system "vagrant plugin install #{plugin}"
exec "vagrant #{ARGV.join(" ")}"
end
end
begin
Dotenv.load
rescue => e
puts 'problem loading dotenv'
puts e
exit 1
end
begin
require 'deep_merge'
rescue => e
puts 'problem loading deep_merge'
puts e
exit 1
end
require 'yaml'
require 'pathname'
require 'fileutils'
DIR = Pathname.new(__FILE__).dirname
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = '2'
ROOT_DIR='/opt/puppet'
## These environment variables allows the Vagrant Environment to be used to
## prototype any environment at runtime. The same functionality allows
## Vagrant to act as a fixture in an environment (to build containers, act as
## CI containers, and clean-room packaging environment)
VM_BOXES = {
:ubuntu => 'puppetlabs/ubuntu-14.04-64-puppet',
:fedora => 'chef/fedora-20',
:centos => 'puppetlabs/centos-6.5-64-puppet',
}
VM_ENV = ENV['environment'] || 'current_working_directory'
PROVISIONER = ENV['provisioner'] || 'puppet-apply'
module Vagrant
class Stack
require 'yaml'
attr_reader :defaults
attr_accessor :stack
def initialize
@servers = {}
@defaults = defaults
@stack = 'default'
end
def defaults
@defaults ||= {
'memory' => ENV['memory'] || 2048,
'cpus' => ENV['cpus'] || 1,
'puppet' => {
'facts' => {
'role' => ENV['role'] || 'default',
'container' => ENV['container'] || nil,
'datacenter' => ENV['container'].nil? ? 'vagrant' : 'docker',
},
},
'hostname' => ENV['hostname'] || 'vagrant',
'box' => ENV['box'].nil? ? VM_BOXES[:ubuntu] : VM_BOXES[ENV['box'].to_sym],
'domain' => ENV['domain'] || 'stackstorm.net',
'sync_type' => ENV['sync_type'] || 'rsync',
'do' => {
'ssh_key_path' => ENV['DO_SSH_KEY_PATH'] || '~/.ssh/id_rsa',
'token' => ENV['DO_TOKEN'],
'image' => '14.04 x64',
'region' => 'nyc3',
'size' => '1gb',
},
'aws' => {
'access_key' => ENV['AWS_ACCESS_KEY'],
'secret_access_key' => ENV['AWS_SECRET_ACCESS_KEY'],
'keypair_name' => ENV['AWS_KEYPAIR_NAME'],
'ssh_key_path' => ENV['AWS_SSH_KEY_PATH'] || '~/.ssh/id_rsa',
'ami' => ENV['AWS_AMI'] || 'ami-29ebb519',
'username' => ENV['AWS_USERNAME'] || 'ubuntu',
'region' => ENV['AWS_REGION'] || 'us-west-2',
},
'ssh' => {
'pty' => false,
'forward_agent' => true,
},
}
end
def servers
@servers.delete_if {|k, v| k == 'defaults' }
end
def add_server(server, config = {})
@servers[server] = config.deep_merge(defaults)
end
def load_stack(stack)
@stack = ENV['stack'] || 'st2'
stack_dir = ENV['stack_dir'] || "#{DIR}/stacks"
stack_file = "#{stack_dir}/#{@stack}.yaml"
begin
yaml = YAML::load_file(stack_file)
yaml.each { |server, config| add_server(server, config) }
rescue => e
puts e.message
end
end
end
end
## Load up a pre-defined Stack for development
@stack = Vagrant::Stack.new
unless ENV['stack'].nil?
@stack.load_stack(ENV['stack'])
else
## Defaults for machine. All configurable at runtime as a cleanroom.
@stack.add_server('default')
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |vagrant|
@stack.servers.each do |node, config|
vagrant.vm.define node do |n|
n.vm.box = config['box']
n.vm.hostname = "#{config['hostname']}.#{config['domain']}"
n.ssh.forward_agent = config['ssh']['forward_agent'] || true
n.ssh.pty = config['ssh']['pty'] || false
n.vm.provider 'vmware_fusion' do |vmware|
vmware.vmx['memsize'] = config['memory'].to_s
vmware.vmx['numvcpus'] = config['cpus'].to_s
end
n.vm.provider 'vmware_workstation' do |vmware|
vmware.vmx['memsize'] = config['memory'].to_s
vmware.vmx['numvcpus'] = config['cpus'].to_s
end
n.vm.provider 'virtualbox' do |virtualbox|
virtualbox.memory = config['memory'].to_i
virtualbox.cpus = config['cpus'].to_i
end
n.vm.provider 'digital_ocean' do |digitalocean, override|
override.ssh.private_key_path = config['do']['ssh_key_path']
digitalocean.token = config['do']['token']
digitalocean.image = config['do']['image']
digitalocean.region = config['do']['region']
digitalocean.size = config['do']['size']
end
n.vm.provider 'aws' do |aws, override|
override.vm.box = 'dummy'
aws.region = config['aws']['region']
aws.access_key_id = config['aws']['access_key']
aws.secret_access_key = config['aws']['secret_access_key']
aws.keypair_name = config['aws']['keypair_name']
aws.ami = config['aws']['ami']
override.ssh.username = config['aws']['username']
override.ssh.private_key_path = config['aws']['ssh_key_path']
end
if config.has_key?('private_networks')
config['private_networks'].each do |nic|
n.vm.network 'private_network', ip: nic
end
end
# Sync up any file mounts for you
if config.has_key?('mounts')
config['mounts'].each do |mount|
vm_mount, local_mount = mount.split(/:/)
local_mount ||= [DIR, 'mounts', vm_mount.gsub(/\//, '_')].join('/')
FileUtils.mkdir_p local_mount
n.vm.synced_folder local_mount, vm_mount, type: config['sync_type']
if Vagrant.has_plugin?('vagrant-bindfs')
n.bindfs.bind_folder vm_mount, vm_mount
end
end
end
## Bootstrap using different provisioners.
case PROVISIONER
when 'puppet-apply' then
n.vm.synced_folder '.', ROOT_DIR, type: config['sync_type'],
rsync__exclude: ['.bundle/', 'vendor/bundle', 'artifacts', 'graphs',
'packer', '.tmp/', '.librarian/', 'src/', '.bundler',
'.puppetfile', 'bin', 'environments']
n.vm.provision 'shell', inline: "#{ROOT_DIR}/script/bootstrap-os"
n.vm.provision 'shell', inline: <<-EOF
# Skips the `git pull` step, since you're working out of it directly
export DISABLE_GIT=true
# Use the current branch by default as the envirnment. Override with environment=XXX
export ENV=current_working_directory
# Do not update Gems/Puppetfile/Environments each run
export CACHE_LIBRARIES=true
# Notify this is a development workspace
export WS_ENV=development
# Pass through Debug Commands
export DEBUG=#{ENV['DEBUG']}
# Collect facts for reference within puppet
#{config['puppet']['facts'].collect { |k,v| "export FACTER_#{k}=#{v}"}.join("\n")}
export FACTER_stack=#{@stack.stack}
#{ROOT_DIR}/script/puppet-apply
EOF
# Only run self-check if explicitly asked via Stack
if config.has_key?('self-check')
n.vm.provision 'shell', inline: "#{ROOT_DIR}/script/check-st2-ok"
end
when 'ansible-local' then
n.vm.provision 'shell', inline: '/vagrant/script/bootstrap-ansible'
n.vm.provision 'shell', inline: <<-EOF
# Use the current branch by default as the envirnment. Override with environment=XXX
export ENV=#{VM_ENV}
# Do not update Gems/Puppetfile/Environments each run
export CACHE_LIBRARIES=true
# Notify this is a development workspace
export WS_ENV=development
# Pass through Debug Commands
export DEBUG=#{ENV['DEBUG']}
# (Re)initialize the repo
git init /opt/ansible
pushd /opt/ansible
# Add remote
git remote add origin https://github.com/stackstorm/ansible-st2 2> /dev/null ||
echo "Remote origin already exists"
# Try to detach HEAD
git checkout --detach -q 2> /dev/null &&
# if HEAD exist, try to make a fast-forward update of master
echo "Fetching latest changes from master branch..." &&
(git fetch origin master:master ||
echo "Fast-forward on master branch is not possible. Please, reset it manually.") &&
# then attach the HEAD back
git checkout - -q ||
# otherwise, it is likely a new repository, so just create a new master branch off origin
( git fetch origin; git checkout master )
# Finally, run playbook locally
env PYTHONUNBUFFERED=1 ansible-playbook #{config['ansible']['playbook']} -i 'localhost,' --connection=local
popd
EOF
if config.has_key?('self-check')
n.vm.provision 'shell', inline: "#{ROOT_DIR}/script/check-st2-ok"
end
else
puts "Unsupported provisioner: #{PROVISIONER}. Skipping..."
end
end
end
end