Class: RsVagrantShim::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/rs_vagrant_shim/cli.rb

Instance Method Summary collapse

Instance Method Details

#init(projectname) ⇒ Object



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
# File 'lib/rs_vagrant_shim/cli.rb', line 32

def init(projectname)
  if File.directory? projectname
    puts "A directory named #{projectname} already exists, please specify a different project name"
    return
  else
    FileUtils.mkdir_p projectname
  end

  vagrantfile_template = <<-EOF
require 'berkshelf/vagrant'
require 'rs_vagrant_shim'

Vagrant::Config.run do |config|
  <% idx = 0 %><% boxes.each do |vmname| %>config.vm.define :<%= vmname %>_centos do |<%= vmname %>_centos_config|
<%= vmname %>_centos_config.berkshelf.berksfile_path = "Berksfile"

<%= vmname %>_centos_config.vm.host_name = "<%= vmname %>_centos"

<%= vmname %>_centos_config.vm.box = "ri_centos6.3_v5.8.8"
<%= vmname %>_centos_config.vm.box_url = "https://s3.amazonaws.com/rgeyer/pub/ri_centos6.3_v5.8.8_vagrant.box"

<%= vmname %>_centos_config.vm.network :hostonly, "33.33.33.<%= 10 + idx %>"

<%= vmname %>_centos_config.ssh.max_tries = 40
<%= vmname %>_centos_config.ssh.timeout   = 120

<%= vmname %>_centos_config.vm.provision Vagrant::RsVagrantShim::Provisioners::RsVagrantShim do |chef|
  chef.run_list_dir = "runlists/<%= vmname %>_centos"
  chef.shim_dir = "rs_vagrant_shim/<%= vmname %>_centos"
end

<% if options[:storage] %>hdd_file = "<%= vmname %>_centos_storage.vdi"
unless File.exists?(hdd_file)
  <%= vmname %>_centos_config.vm.customize [
    "createhd",
    "--filename", hdd_file,
    "--size", 10240
  ]
  <%= vmname %>_centos_config.vm.customize [
    "storageattach",
    :id,
    "--storagectl", "SATA Controller",
    "--port", 1,
    "--type", "hdd",
    "--medium", hdd_file
  ]
end<% end %><% idx += 1 %>
  end

  config.vm.define :<%= vmname %>_ubuntu do |<%= vmname %>_ubuntu_config|
<%= vmname %>_ubuntu_config.berkshelf.berksfile_path = "Berksfile"

<%= vmname %>_ubuntu_config.vm.host_name = "<%= vmname %>_ubuntu"

<%= vmname %>_ubuntu_config.vm.box = "ri_ubuntu12.04_v5.8.8"
<%= vmname %>_ubuntu_config.vm.box_url = "https://s3.amazonaws.com/rgeyer/pub/ri_ubuntu12.04_v5.8.8_vagrant.box"

<%= vmname %>_ubuntu_config.vm.network :hostonly, "33.33.33.<%= 10 + idx %>"

<%= vmname %>_ubuntu_config.ssh.max_tries = 40
<%= vmname %>_ubuntu_config.ssh.timeout   = 120

<%= vmname %>_ubuntu_config.vm.provision Vagrant::RsVagrantShim::Provisioners::RsVagrantShim do |chef|
  chef.run_list_dir = "runlists/<%= vmname %>_ubuntu"
  chef.shim_dir = "rs_vagrant_shim/<%= vmname %>_ubuntu"
end

<% if options[:storage] %>hdd_file = "<%= vmname %>_ubuntu_storage.vdi"
unless File.exists?(hdd_file)
  <%= vmname %>_ubuntu_config.vm.customize [
    "createhd",
    "--filename", hdd_file,
    "--size", 10240
  ]
  <%= vmname %>_ubuntu_config.vm.customize [
    "storageattach",
    :id,
    "--storagectl", "SATA Controller",
    "--port", 1,
    "--type", "hdd",
    "--medium", hdd_file
  ]
end<% end %><% idx += 1 %>
  end<% end %>
end
  EOF

  default_runlist_template = <<-EOF
{
  "cloud": { "provider": "vagrant", "public_ips": [], "private_ips": [] },
  "rightscale": { "instance_uuid": "uuid-<%= box %>" },
  "run_list": [
"recipe[rs_vagrant_shim]"
  ]
}
  EOF

  vagrantfile_erb = ERB.new(vagrantfile_template)
  boxes = ["default"]
  boxes = options[:vmnames] if options[:vmnames] && !options[:vmnames].empty?
  File.open(File.join(projectname, "Vagrantfile"), "w") do |file|
    file.write(vagrantfile_erb.result(binding))
  end

  boxes.each do |box|
    runlist_dir = File.join(projectname, "runlists", "#{box}_centos")
    shim_dir = File.join(projectname, "rs_vagrant_shim", "#{box}_centos")
    FileUtils.mkdir_p runlist_dir unless File.directory? runlist_dir
    FileUtils.mkdir_p shim_dir unless File.directory? shim_dir
    default_runlist_file = File.join(runlist_dir, "default.json")
    default_runlist_erb = ERB.new(default_runlist_template)
    File.open(File.join(default_runlist_file), "w") do |file|
      file.write(default_runlist_erb.result(binding))
    end
  end

  boxes.each do |box|
    runlist_dir = File.join(projectname, "runlists", "#{box}_ubuntu")
    shim_dir = File.join(projectname, "rs_vagrant_shim", "#{box}_ubuntu")
    FileUtils.mkdir_p runlist_dir unless File.directory? runlist_dir
    FileUtils.mkdir_p shim_dir unless File.directory? shim_dir
    default_runlist_file = File.join(runlist_dir, "default.json")
    default_runlist_erb = ERB.new(default_runlist_template)
    File.open(File.join(default_runlist_file), "w") do |file|
      file.write(default_runlist_erb.result(binding))
    end
  end

  # This is where the gemfile definition is copied from the .gemspec, probably
  # need to store these in a single file and source them from both locations
  File.open(File.join(projectname, "Gemfile"), "w") do |file|
    file.write <<-EOF
source :rubygems

# This is probably a bad idea while rs_vagrant_shim is evolving, you might wanna consider specifying
# a specific rs_vagrant_shim version
gem "rs_vagrant_shim", "~> 0.2.0"

gem "berkshelf", "~> 1.1"
gem "vagrant", "~> 1.0.5"
    EOF
  end

  File.open(File.join(projectname, "Berksfile"), "w") do |file|
    file.write <<-EOF
site :opscode

cookbook "rightscale",
  git: "git://github.com/rightscale/rightscale_cookbooks.git",
  branch: "v13.2",
  rel: "cookbooks/rightscale"

cookbook "sys",
  git: "git://github.com/rightscale/rightscale_cookbooks.git",
  branch: "v13.2",
  rel: "cookbooks/sys"

cookbook "sys_firewall",
  git: "git://github.com/rightscale/rightscale_cookbooks.git",
  branch: "v13.2",
  rel: "cookbooks/sys_firewall"

group :vagrant_only do
  cookbook "rs_vagrant_shim",
git: "https://github.com/rgeyer-rs-cookbooks/rs_vagrant_shim.git",
rel: "cookbooks/rs_vagrant_shim"
end
    EOF
  end
end