-
Notifications
You must be signed in to change notification settings - Fork 11
/
Rakefile
179 lines (155 loc) · 5.5 KB
/
Rakefile
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
class String
# Strip leading whitespace from each line that is the same as the
# amount of whitespace on the first line of the string.
# Leaves _additional_ indentation on later lines intact.
# SEE: http://stackoverflow.com/a/5638187/504018
def unindent
gsub /^#{self[/\A\s*/]}/, ''
end
end
desc "Prepare Ariadne environmnet to spin up a project.
This runs librarian-chef to install cookbooks, creates properly-permissioned
temporary vagrant-dns files, and creates several empty directories required for
sharing with the VM."
task :setup do
# Write the config file if doesn't exist.
unless File.exists?("config/config.yml")
p "Creating config/config.yml..."
config = File.open("config/config.yml", "w")
config.puts <<-EOF.unindent
---
basebox: lucid64
project: example
branch: develop
memory: 1000
cpu_count: 2
# If building an install profile according to Myplanet layout assumptions,
# enter its repository URL here. (Otherwise, leave blank.)
repo_url: ''
EOF
config.close
end
p "Installing cookbooks using Librarian gem..."
system "librarian-chef install"
if RUBY_PLATFORM =~ /darwin/
p "Starting vagrant-dns server..."
p "(You may be prompted for your system password.)"
system "vagrant dns --restart"
system "rvmsudo vagrant dns --install"
end
rel_dirs = %w{
tmp/apt/cache/partial
tmp/drush/cache
data/html
data/make
data/profiles
cookbooks-projects
}
p "Creating required directories:"
rel_dirs.each do |rel_dir|
p rel_dir
abs_dir = File.join(Dir::pwd, rel_dir)
FileUtils.mkdir_p abs_dir
end
end
desc "Import an Ariadne project from an external git repo.
If the repo name is prefixed with 'ariadne-', this will be stripped before
cloning to `cookbooks-projects`."
task :init_project, [:repo] do |t, args|
repository_name = args.repo.sub(/\.git$/, '').split(/([\w-]+)?$/)[-1]
project_name = repository_name.sub(/^ariadne-/, '')
system "git clone #{args.repo} cookbooks-projects/#{project_name}"
end
desc "Restarts the network service inside the VM.
This often needs to be run when you've changes wifi hotspots or have been
disconnected temporily. If the VM is taking a long to time provision, or timing
out, run this task."
task :fix_network do
require 'vagrant'
env = Vagrant::Environment.new
env.vms.each do |id, vm|
raise Vagrant::Errors::VMNotCreatedError if !vm.created?
raise Vagrant::Errors::VMNotRunningError if vm.state != :running
vm.channel.sudo("/etc/init.d/networking restart")
end
end
desc "Clear DNS cache and restart resolver. (OSX only!)
Sometimes the Mac's DNS resolver controlled by vagrant-dns can go wonky. This
will clear the Mac's DNS cache and restart the vagrant-dns resolver."
task :restart_dns do
# Raise exception immediately unless running OSX
raise "vagrant-dns only works on OSX!" unless RUBY_PLATFORM =~ /darwin/
p "Uninstalled DNS resolver..."
p "(You may be prompted for your system password.)"
system "rvmsudo vagrant dns --uninstall"
p "Removing temporary files..."
system "rm -r ~/.vagrant.d/tmp/dns"
p "Restarting DNS server..."
system "vagrant dns --restart"
p "Re-installing DNS resolver..."
system "rvmsudo vagrant dns --install"
p "Flushing OSX DNS cache..."
system "scacheutil -flushcache"
end
desc "Transfers your user-specific .gitconfig to the VM.
While this will transfer lots of personal settings into the VM, it perhaps most
importantly ensures that your commits are tied to your name and email. This
will ensure that your commits are properly linked your GitHub account."
task :send_gitconfig do
require 'vagrant'
env = Vagrant::Environment.new
env.vms.each do |id, vm|
raise Vagrant::Errors::VMNotCreatedError if !vm.created?
raise Vagrant::Errors::VMNotRunningError if vm.state != :running
ssh_info = vm.ssh.info
ssh_host = ssh_info[:host]
ssh_port = ssh_info[:port]
ssh_user = ssh_info[:username]
private_key_path = ssh_info[:private_key_path]
system "scp -i #{private_key_path} -P #{ssh_port} ~/.gitconfig #{ssh_user}@#{ssh_host}:~"
raise "Could not find .gitconfig in home directory (~)." if $? != 0
puts ".gitconfig sent to #{vm.name} VM!"
end
end
desc "WARNING! Will bring Ariadne back to a pristine state, good-as-new.
This will:
* uninstall the DNS resolver and tmp vagrant-dns files
* destroy the VM
* delete cookbooks, gems, apt packages, project-specific data & tmp dns files
* remove ariadne ruby version and source"
task :fresh_start do
system "rvmsudo vagrant dns --uninstall"
system "vagrant destroy --force"
system "rm -r ~/.vagrant.d/tmp/dns"
system "rm -rf tmp/ cookbooks/ .bundle/"
system "chmod -R u+w data/html/; rm -rf data/html data/make cookbooks-projects/*;"
system "rvm remove 1.9.3-p194-ariadne"
end
namespace :test do
desc "Runs foodcritic linter"
task :foodcritic do
if Gem::Version.new("1.9.2") <= Gem::Version.new(RUBY_VERSION.dup)
cmd = %w{
bundle exec foodcritic cookbooks*/*
--include test/support/foodcritic/*
--epic-fail any
--tags ~CINK001
--tags ~FC011
--tags ~FC031
}
sh cmd.join(" ")
else
puts "WARN: foodcritic run is skipped as Ruby #{RUBY_VERSION} is < 1.9.2."
end
end
desc "Runs knife cookbook test"
task :knife do
cmd = %w{
bundle exec knife cookbook test
$(ls -m cookbooks-override | tr -d ',')
$(ls -m cookbooks-projects | tr -d ',')
--config=test/support/knife.rb
}
sh cmd.join(" ")
end
end