Class: BoxGrinder::BbcloudPlatformPlugin

Inherits:
BasePlugin
  • Object
show all
Defined in:
lib/platform/bbcloud-platform-plugin.rb

Instance Method Summary collapse

Instance Method Details

#add_default_user(guestfs) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/platform/bbcloud-platform-plugin.rb', line 83

def add_default_user(guestfs)
  @log.debug "Adding #{@plugin_config['username']} user..."
  unless guestfs.fgrep(@plugin_config['username'], "/etc/passwd").empty?
    @log.debug("#{@plugin_config['username']} already exists, skipping.")
    return
  end
  guestfs.sh("useradd #{@plugin_config['username']}")
  guestfs.sh("echo -e '#{@plugin_config['username']}\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers")
  @log.debug "User #{@plugin_config['username']} added."
end

#after_initObject



27
28
29
30
31
# File 'lib/platform/bbcloud-platform-plugin.rb', line 27

def after_init
  register_deliverable(:disk => "#{deliverable_name}.qcow2")
  set_default_config_value('username', default_user)
  @appliance_config.packages |= %w{cloud-init} if has_cloud_init?
end

#change_ssh_configuration(guestfs_helper) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/platform/bbcloud-platform-plugin.rb', line 120

def change_ssh_configuration(guestfs_helper)
  guestfs_helper.augeas do
    # disable password authentication
    set("/etc/ssh/sshd_config", "PasswordAuthentication", "no")

    # disable root login
    set("/etc/ssh/sshd_config", "PermitRootLogin", "no")

	# Switch off GSS Authentication
    set("/etc/ssh/sshd_config", "GSSAPIAuthentication", "no")
  end
end

#customise_cloud_init(guestfs_helper) ⇒ Object



60
61
62
# File 'lib/platform/bbcloud-platform-plugin.rb', line 60

def customise_cloud_init(guestfs_helper)
  guestfs_helper.sh("sed -i 's/^user: ec2-user$/user: #{@plugin_config['username']}/' /etc/cloud/cloud.cfg")
end

#default_userObject



75
76
77
# File 'lib/platform/bbcloud-platform-plugin.rb', line 75

def default_user
  "brightbox"
end

#deliverable_nameObject



79
80
81
# File 'lib/platform/bbcloud-platform-plugin.rb', line 79

def deliverable_name
  "#{@appliance_config.name}-#{@appliance_config.version}.#{@appliance_config.release}-#{@appliance_config.os.name}-#{@appliance_config.os.version}"
end

#disable_root_password(guestfs_helper) ⇒ Object



94
95
96
97
98
# File 'lib/platform/bbcloud-platform-plugin.rb', line 94

def disable_root_password(guestfs_helper)
  @log.debug "Disabling root password"
  guestfs_helper.sh("usermod -L root")
  @log.info "root password disabled - use sudo from #{@plugin_config['username']} account"
end

#executeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/platform/bbcloud-platform-plugin.rb', line 33

def execute
  @log.info "Converting #{@appliance_config.name} appliance image to use Brightbox metadata services"

  @log.debug "Using qemu-img to convert the image to qcow2 format..."
  @image_helper.convert_disk(@previous_deliverables.disk, :qcow2, @deliverables.disk)
  @log.debug "Conversion done."

  @log.debug "Adding metadata customisations"
  @image_helper.customize([@deliverables.disk], :automount => true) do |guestfs, guestfs_helper|
    add_default_user(guestfs)
	disable_root_password(guestfs_helper)
	set_default_timezone(guestfs_helper)
    change_ssh_configuration(guestfs_helper)
	if has_cloud_init?
	  customise_cloud_init(guestfs_helper)
	else
	  update_rc_local(guestfs)
	end
    execute_post(guestfs_helper)
  end
  @log.debug "Added customisations"
end

#execute_post(guestfs_helper) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/platform/bbcloud-platform-plugin.rb', line 64

def execute_post(guestfs_helper)
  if @appliance_config.post['bbcloud']
    @appliance_config.post['bbcloud'].each do |cmd|
      guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
    end
    @log.debug "Post commands from appliance definition file executed."
  else
    @log.debug "No Post commands specified, skipping."
  end
end

#has_cloud_init?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/platform/bbcloud-platform-plugin.rb', line 56

def has_cloud_init?
  @appliance_config.os.name == 'fedora' and @appliance_config.os.version >= '16'
end

#set_default_timezone(guestfs_helper) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/platform/bbcloud-platform-plugin.rb', line 133

def set_default_timezone(guestfs_helper)
  guestfs_helper.augeas do
    set("/etc/sysconfig/clock", "UTC", "True")
    set("/etc/sysconfig/clock", "ZONE", "Etc/UTC")
  end
  guestfs_helper.guestfs.cp("/usr/share/zoneinfo/UTC", "/etc/localtime")
end

#update_rc_local(guestfs) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/platform/bbcloud-platform-plugin.rb', line 100

def update_rc_local(guestfs)
  @log.debug "Updating '/etc/rc.d/rc.local' file..."
  rc_local = Tempfile.new('rc_local')

  if guestfs.exists("/etc/rc.d/rc.local") == 1
    # We're appending
    rc_local << guestfs.read_file("/etc/rc.d/rc.local")
  else
    # We're creating new file
    rc_local << "#!/bin/bash\n\n"
  end

  rc_local << File.read("#{File.dirname(__FILE__)}/src/rc_local")
  rc_local.flush

  guestfs.upload(rc_local.path, "/etc/rc.d/rc.local")

  rc_local.close
end