Class: Bonethug::Installer

Inherits:
Object
  • Object
show all
Includes:
Digest, FileUtils
Defined in:
lib/bonethug/installer.rb

Constant Summary collapse

@@bonthug_gem_dir =
File.expand_path(File.dirname(__FILE__) + '/../..')
@@skel_dir =
@@bonthug_gem_dir + '/skel'
@@conf =
Conf.new.add(@@skel_dir + '/skel.yml')
@@project_config_files =
{editable: ['cnf.yml','schedule.rb'], generated: ['backup.rb','deploy.rb']}

Class Method Summary collapse

Class Method Details

.clean(target) ⇒ Object



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
# File 'lib/bonethug/installer.rb', line 99

def self.clean(target)

  manifest_path = target + '/.bonethug/manifest'

  if File.exists? manifest_path

    puts 'Reading manifest...'
    manifest = File.read(manifest_path).split("\n")

    puts 'Cleaning up ' + manifest.count.to_s + ' files'
    not_removed = []
    manifest.each do |file|
      not_removed.push file unless self.try_delete file
    end

    if not_removed.count > 0

      puts 'Retrying removal of ' + not_removed.count.to_s + ' files'
      failed = []
      not_removed.each do |file|
        failed.push file unless self.try_delete file
      end

      puts 'Removal of the following' + failed.count.to_s + ' files failed'
      puts failed.join("\n")

    end

  else
    puts 'Nothing to do'
  end
  self
end

.execute_init_mysql_db_script(env, admin_user = 'root', admin_pass = '', path = '.') ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/bonethug/installer.rb', line 147

def self.execute_init_mysql_db_script(env, admin_user = 'root', admin_pass = '', path = '.')

  exec_path = File.expand_path(path)
  conf = Bonethug::Conf.new.add(exec_path + '/config/cnf.yml')
  conf.add(exec_path + '/config/database.yml' => { root: 'dbs.default' }) if File.exist? exec_path + '/config/database.yml'

  conf.get('dbs').each do |name,envs|

    db = envs.get env
    if !db
      puts "No db for env " + env + " found - check your config file"
      exit
    else
      puts "Mysql user " + admin_user + " is creating db: " + db.get('name') + " and granting access to " + db.get('user') + "@" + db.get('host') + ", you may be prompted for the password for the user: " + admin_user
      puts "NB: You may be affected by this bug if your admin user pass is longer than 8 chars: http://dev.mysql.com/doc/refman/5.0/en/password-too-long.html"
      system Bonethug::Installer.init_mysql_db_script(db, admin_user, admin_pass)
    end

  end

end

.get_setup_env_cmdsObject



191
192
193
# File 'lib/bonethug/installer.rb', line 191

def self.get_setup_env_cmds
  self.parse_sh self.get_setup_script_content(env)
end

.get_setup_script(env) ⇒ Object

Reads system setup scripts




173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/bonethug/installer.rb', line 173

def self.get_setup_script(env)
  exec_path = File.expand_path(path)
  conf = Bonethug::Conf.new.add(exec_path + '/config/cnf.yml')
  deploy = conf.node_merge 'deploy.common','deploy.environments.' + env
  os_type = 'ubuntu'
  os_version = ''
  if os = deploy.get('os')
    os_type = os.get('type') if os.get('type')
    os_version = os.get('version') if os.get('version')
  end
  os_str = os_type + (os and os_version.to_s.length ? '-' + os_version.to_s : '')
  @@bonthug_gem_dir + '/scripts/' + os_str
end

.get_setup_script_content(env) ⇒ Object



187
188
189
# File 'lib/bonethug/installer.rb', line 187

def self.get_setup_script_content(env)
  File.read self.get_setup_script(env)
end

.init_mysql_db_script(db, admin_user = 'root', admin_pass = '') ⇒ Object

Prepares init db scripts




136
137
138
139
140
141
142
143
144
145
# File 'lib/bonethug/installer.rb', line 136

def self.init_mysql_db_script(db, admin_user = 'root', admin_pass = '')

  script_content = "
    CREATE DATABASE IF NOT EXISTS " + db.get('name') + ";
    GRANT ALL ON " + db.get('name') + ".* TO " + db.get('user') + "@" + db.get('host') + (db.get('pass') ? " IDENTIFIED BY '" + db.get('pass') + "'" : "") + ";
    FLUSH PRIVILEGES;
  "
  cmd = 'echo "' + script_content + '" | mysql -h ' + db.get('host') + ' -u ' + admin_user + ' -p' + admin_pass

end

.install(type, target = '.') ⇒ Object



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
# File 'lib/bonethug/installer.rb', line 29

def self.install(type, target = '.')

  # @@conf = Conf.new.add(@@skel_dir + '/skel.yml')

  # create full path
  target = File.expand_path target

  # let the user know we are installing
  puts 'Installing '+ type + ' to ' + target + '...'

  # load the configuration
  unless @@conf.get('project_types').has_key? type.to_s
    puts "Unsupported type: " + type.to_s
    exit
  end
  conf = @@conf.node_merge 'base', 'project_types.' + type

  # set the tmp dir
  tmp_dir = File.expand_path target + '/.bonethug-tmp'

  # clean up any exisitng install tmp files
  if File.directory? tmp_dir
    puts 'Cleaning up old installer temporary files...'
    FileUtils.rm_rf tmp_dir
  end

  # create tmp dir
  puts 'Creating build directory at ' + tmp_dir
  FileUtils.mkdir tmp_dir
  FileUtils.mkdir tmp_dir + '/.bonethug'

  # build the file set
  puts 'Building ' + type + ' skeleton...'
  FileUtils.cp_r @@skel_dir + '/base/.', tmp_dir
  FileUtils.cp_r @@skel_dir + '/project_types/' + type + '/.', tmp_dir

  # build the manifest
  puts 'Creating manifest...'
  self.build_manifest tmp_dir

  # modify the manifest root
  manifest_path = tmp_dir + '/.bonethug/manifest'
  File.open(manifest_path,'w') do |file|
    file.puts File.read(manifest_path).gsub(/\.bonethug-tmp/,'')
  end

  # clean up the target dir
  puts 'Cleaning up install directory...'
  self.clean target

  # copy the files
  puts 'Installing build to ' + target + '...'
  FileUtils.cp_r tmp_dir + '/.', target

  # try to update the configuration files
  puts 'Updating build informtation...'
  self.(target)

  # clean up any exisitng install tmp files
  puts 'Cleaning up temporary files...'
  FileUtils.rm_rf tmp_dir

  puts "Installation Complete"

  # try to update the configuration files
  puts 'Updating configs...'
  self.bonethugise(target, :init)

end

.parse_sh(content) ⇒ Object



195
196
197
# File 'lib/bonethug/installer.rb', line 195

def self.parse_sh(content)
  content.split("\n").select { |line| !(line =~ /^[\s\t]+$/ || line =~ /^[\s\t]*#/ || line.strip.length == 0) }
end