Class: Bebox::Project

Inherits:
Object
  • Object
show all
Includes:
FilesHelper, Logger
Defined in:
lib/bebox/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FilesHelper

#file_content_trimmed, #generate_file_from_template, included, #render_erb_template, #write_content_to_file

Methods included from Logger

#error, #highline_quest, #highline_warn, included, #info, #linebreak, #msg, #ok, #quest, #title, #warn

Constructor Details

#initialize(name, vagrant_box_base, parent_path, vagrant_box_provider, default_environments = []) ⇒ Project

Returns a new instance of Project.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bebox/project.rb', line 10

def initialize(name, vagrant_box_base, parent_path, vagrant_box_provider, default_environments = [])
  self.name = name
  self.vagrant_box_base = vagrant_box_base
  self.parent_path = parent_path
  self.vagrant_box_provider = vagrant_box_provider
  self.environments = []
  self.path = "#{self.parent_path}/#{self.name}"
  default_environments.each do |env|
    self.environments << Bebox::Environment.new(env, self.path)
  end
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



8
9
10
# File 'lib/bebox/project.rb', line 8

def created_at
  @created_at
end

#environmentsObject

Returns the value of attribute environments.



8
9
10
# File 'lib/bebox/project.rb', line 8

def environments
  @environments
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/bebox/project.rb', line 8

def name
  @name
end

#parent_pathObject

Returns the value of attribute parent_path.



8
9
10
# File 'lib/bebox/project.rb', line 8

def parent_path
  @parent_path
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/bebox/project.rb', line 8

def path
  @path
end

#vagrant_box_baseObject

Returns the value of attribute vagrant_box_base.



8
9
10
# File 'lib/bebox/project.rb', line 8

def vagrant_box_base
  @vagrant_box_base
end

#vagrant_box_providerObject

Returns the value of attribute vagrant_box_provider.



8
9
10
# File 'lib/bebox/project.rb', line 8

def vagrant_box_provider
  @vagrant_box_provider
end

Class Method Details

.name_from_file(project_root) ⇒ Object

Get Project name from the .bebox file



79
80
81
82
# File 'lib/bebox/project.rb', line 79

def self.name_from_file(project_root)
  project_config = YAML.load_file("#{project_root}/.bebox")
  project_config['project']
end

.public_ssh_key_from_file(project_root, environment) ⇒ Object

Obtain the ssh public key from file in environment



226
227
228
229
# File 'lib/bebox/project.rb', line 226

def self.public_ssh_key_from_file(project_root, environment)
  ssh_key_path = "#{project_root}/config/keys/environments/#{environment}/id_rsa.pub"
  return (File.exist?(ssh_key_path)) ? File.read(ssh_key_path).strip : ''
end

.shortname_from_file(project_root) ⇒ Object

Get short project name from the .bebox file



73
74
75
76
# File 'lib/bebox/project.rb', line 73

def self.shortname_from_file(project_root)
  project_name = self.name_from_file(project_root)
  project_name.gsub("bebox-", "")
end

.so_dependenciesObject



192
193
194
# File 'lib/bebox/project.rb', line 192

def self.so_dependencies
  File.read("#{Bebox::Project.templates_path}/project/ubuntu_dependencies").gsub(/\s+/, ' ')
end

.templates_pathObject

Path to the templates directory in the gem



220
221
222
223
# File 'lib/bebox/project.rb', line 220

def self.templates_path
  # File.expand_path(File.join(File.dirname(__FILE__), "..", "gems/bundler/lib/templates"))
  File.join((File.expand_path '..', File.dirname(__FILE__)), 'templates')
end

.vagrant_box_base_from_file(project_root) ⇒ Object

Get Project vagrant box base from the .bebox file



67
68
69
70
# File 'lib/bebox/project.rb', line 67

def self.vagrant_box_base_from_file(project_root)
  project_config = YAML.load_file("#{project_root}/.bebox")
  project_config['vagrant_box_base']
end

.vagrant_box_provider_from_file(project_root) ⇒ Object

Get Project vagrant box provider from the .bebox file



61
62
63
64
# File 'lib/bebox/project.rb', line 61

def self.vagrant_box_provider_from_file(project_root)
  project_config = YAML.load_file("#{project_root}/.bebox")
  project_config['vagrant_box_provider']
end

Instance Method Details

#bundle_projectObject

Bundle install packages for project



202
203
204
# File 'lib/bebox/project.rb', line 202

def bundle_project
  system("cd #{self.path} && BUNDLE_GEMFILE=Gemfile bundle install")
end

#copy_default_roles_profilesObject

Create the default base roles and profiles in the project



111
112
113
114
115
116
117
118
# File 'lib/bebox/project.rb', line 111

def copy_default_roles_profiles
  # Copy default roles and profiles to project templates directory
  `cp -R #{Bebox::Project.templates_path}/puppet/default_roles/* #{self.path}/templates/roles/`
  `cp -R #{Bebox::Project.templates_path}/puppet/default_profiles/* #{self.path}/templates/profiles/`
  # Copy default roles and profiles to project roles and profiles available
  `cp -R #{Bebox::Project.templates_path}/puppet/default_roles/* #{self.path}/puppet/roles/`
  `cp -R #{Bebox::Project.templates_path}/puppet/default_profiles/* #{self.path}/puppet/profiles/`
end

#copy_puppet_install_filesObject

Copy puppet install files



178
179
180
181
# File 'lib/bebox/project.rb', line 178

def copy_puppet_install_files
  `cd #{self.path} && mkdir -p puppet/lib/deb`
  `cp -R #{lib_path}/deb/* #{self.path}/puppet/lib/deb/`
end

#createObject

Project creation phase



23
24
25
26
27
28
29
30
# File 'lib/bebox/project.rb', line 23

def create
	create_project_directory
  create_puppet_base
  create_project_config
  create_checkpoints
  info "Bundle project ..."
  bundle_project
end

#create_capfileObject

Create Capfile for the project



131
132
133
# File 'lib/bebox/project.rb', line 131

def create_capfile
  write_content_to_file("#{path}/Capfile", File.read("#{Bebox::Project.templates_path}/project/Capfile.erb"))
end

#create_checkpointsObject

Create checkpoints base directories



197
198
199
# File 'lib/bebox/project.rb', line 197

def create_checkpoints
  `cd #{self.path} && mkdir -p .checkpoints/environments`
end

#create_config_deploy_directoriesObject

Create config deploy and keys directories



121
122
123
# File 'lib/bebox/project.rb', line 121

def create_config_deploy_directories
  `cd #{self.path} && mkdir -p config/{deploy/steps,keys/environments}`
end

#create_default_environmentsObject

Create the default environments



126
127
128
# File 'lib/bebox/project.rb', line 126

def create_default_environments
  self.environments.map{|environment| environment.create}
end

#create_gemfileObject

Create Gemfile for the project



136
137
138
# File 'lib/bebox/project.rb', line 136

def create_gemfile
  write_content_to_file("#{self.path}/Gemfile", File.read("#{Bebox::Project.templates_path}/project/Gemfile.erb"))
end

#create_project_configObject

Generate project config files



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bebox/project.rb', line 43

def create_project_config
  # Create deploy directories
  create_config_deploy_directories
  # Generate dot files
  generate_dot_bebox_file
  generate_gitignore_file
  # Generate ruby version file
  generate_ruby_version
  # Generate Capfile and deploy files
  create_capfile
  generate_deploy_files
  # Generate Gemfile
  create_gemfile
  # Create the default environments
  create_default_environments
end

#create_project_directoryObject

Create project directory



38
39
40
# File 'lib/bebox/project.rb', line 38

def create_project_directory
  `mkdir -p #{self.parent_path}/#{self.name}`
end

#create_puppet_baseObject

Create puppet base directories and files



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/bebox/project.rb', line 141

def create_puppet_base
  # Create templates directories
  create_templates_directories
  # Generate SO dependencies files
  generate_so_dependencies_files
  # Copy puppet install files
  copy_puppet_install_files
  # Generate steps directories
  generate_steps_directories
  # Generate steps templates
  generate_steps_templates
  # Copy the default_roles and default_profiles to project
  copy_default_roles_profiles
end

#create_templates_directoriesObject

Create templates directories



106
107
108
# File 'lib/bebox/project.rb', line 106

def create_templates_directories
  `cd #{self.path} && mkdir -p templates/{roles,profiles}`
end

#destroyObject

Delete all files referent to a project



232
233
234
# File 'lib/bebox/project.rb', line 232

def destroy
  `cd #{self.parent_path} && rm -rf #{self.name}`
end

#generate_deploy_filesObject

Generate the deploy file for the project



207
208
209
210
211
212
# File 'lib/bebox/project.rb', line 207

def generate_deploy_files
  generate_file_from_template("#{Bebox::Project.templates_path}/project/config/deploy.erb", "#{self.path}/config/deploy.rb", {project: self})
  Bebox::PROVISION_STEPS.each do |step|
    generate_file_from_template("#{Bebox::Project.templates_path}/project/config/deploy/steps/#{step}.erb", "#{self.path}/config/deploy/steps/#{step}.rb", {})
  end
end

#generate_dot_bebox_fileObject

Generate .bebox file



93
94
95
96
97
98
# File 'lib/bebox/project.rb', line 93

def generate_dot_bebox_file
  # Set the creation time for the project
  self.created_at = DateTime.now.to_s
  # Create the .bebox file from template
  generate_file_from_template("#{Bebox::Project.templates_path}/project/dot_bebox.erb", "#{self.path}/.bebox", {project: self})
end

#generate_gitignore_fileObject

Generate .gitignore file



101
102
103
# File 'lib/bebox/project.rb', line 101

def generate_gitignore_file
  generate_file_from_template("#{Bebox::Project.templates_path}/project/gitignore.erb", "#{self.path}/.gitignore", {steps: Bebox::PROVISION_STEP_NAMES})
end

#generate_ruby_versionObject

Create rbenv local



85
86
87
88
89
90
# File 'lib/bebox/project.rb', line 85

def generate_ruby_version
  ruby_version = (RUBY_PATCHLEVEL == 0) ? RUBY_VERSION : "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
  File.open("#{self.path}/.ruby-version", 'w') do |f|
    f.write ruby_version
  end
end

#generate_so_dependencies_filesObject

Generate SO dependencies files



184
185
186
187
188
189
190
# File 'lib/bebox/project.rb', line 184

def generate_so_dependencies_files
  `cd #{self.path} && mkdir -p puppet/prepare/dependencies/ubuntu`
  ubuntu_dependencies_content = File.read("#{Bebox::Project.templates_path}/project/ubuntu_dependencies")
  File::open("#{self.path}/puppet/prepare/dependencies/ubuntu/packages", "w")do |f|
    f.write(ubuntu_dependencies_content)
  end
end

#generate_steps_directoriesObject

Generate steps directories



157
158
159
160
# File 'lib/bebox/project.rb', line 157

def generate_steps_directories
  Bebox::PROVISION_STEP_NAMES.each{|step| `cd #{self.path} && mkdir -p puppet/steps/#{step}/{hiera/data,manifests,modules}`}
  `cd #{self.path} && mkdir -p puppet/{roles,profiles}`
end

#generate_steps_templatesObject

Generate steps templates for hiera and manifests files



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/bebox/project.rb', line 163

def generate_steps_templates
  Bebox::PROVISION_STEPS.each do |step|
    ssh_key = ''
    step_dir = Bebox::Provision.step_name(step)
    templates_path = Bebox::Project::templates_path
    # Generate site.pp template
    generate_file_from_template("#{templates_path}/puppet/#{step}/manifests/site.pp.erb", "#{self.path}/puppet/steps/#{step_dir}/manifests/site.pp", {nodes: []})
    # Generate hiera.yaml template
    generate_file_from_template("#{templates_path}/puppet/#{step}/hiera/hiera.yaml.erb", "#{self.path}/puppet/steps/#{step_dir}/hiera/hiera.yaml", {step_dir: step_dir})
    # Generate common.yaml template
    generate_file_from_template("#{templates_path}/puppet/#{step}/hiera/data/common.yaml.erb", "#{self.path}/puppet/steps/#{step_dir}/hiera/data/common.yaml", {step_dir: step_dir, ssh_key: ssh_key, project_name: self.shortname})
  end
end

#lib_pathObject

Path to the lib directory in the gem



215
216
217
# File 'lib/bebox/project.rb', line 215

def lib_path
  File.expand_path '..', File.dirname(__FILE__)
end

#shortnameObject

Obtain the project name without ‘bebox-’ prefix



33
34
35
# File 'lib/bebox/project.rb', line 33

def shortname
  self.name.gsub("bebox-", "")
end