Module: Lxc::Helpers::Copies

Included in:
Clone, Ephemeral
Defined in:
lib/elecksee/helpers/copies.rb

Overview

Container related file copy helpers

Constant Summary collapse

NAME_FILES =

Files requiring name updates

%w(fstab config)
HOSTNAME_FILES =

Files requiring hostname updates

%w(
  rootfs/etc/hostname
  rootfs/etc/hosts
  rootfs/etc/sysconfig/network
  rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
)

Instance Method Summary collapse

Instance Method Details

#apply_custom_networkingTrueClass

Apply custom networking files depending on platform

Returns:

  • (TrueClass)


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
# File 'lib/elecksee/helpers/copies.rb', line 97

def apply_custom_networking
  if(el_platform?)
    path = lxc.rootfs.join('etc/sysconfig/network-scripts/ifcfg-eth0')
    content = <<-EOF
DEVICE=eth0
BOOTPROTO=static
NETMASK=#{netmask}
IPADDR=#{ipaddress}
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no
GATEWAY=#{gateway}
EOF
    write_file(path, content)
    path = lxc.rootfs.join('etc/sysconfig/network')
    content = <<-EOF
NETWORKING=yes
HOSTNAME=#{hostname}
EOF
    write_file(path, content)
    write_file(lxc.rootfs.join('etc/rc.local'), "hostname #{hostname}\n")
  else
    path = lxc.rootfs.join('etc/network/interfaces')
    content = <<-EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address #{ipaddress}
netmask #{netmask}
gateway #{gateway}
EOF
    write_file(path, content)
  end
  true
end

#el_platform?TrueClass, FalseClass

Container is Enterprise linux (redhat)

Returns:

  • (TrueClass, FalseClass)


90
91
92
# File 'lib/elecksee/helpers/copies.rb', line 90

def el_platform?
  lxc.rootfs.join('etc/redhat-release').exist?
end

#update_naming(*args) ⇒ TrueClass

Note:

use :no_$FILE where $FILE is the basename to skip

Update container names and host names

Parameters:

  • args (Symbol)

    argument list

Returns:

  • (TrueClass)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/elecksee/helpers/copies.rb', line 71

def update_naming(*args)
  NAME_FILES.each do |file|
    next unless File.exists?(lxc.path.join(file))
    next if args.include?("no_#{file}".to_sym)
    contents = File.read(lxc.path.join(file)).gsub(original, name)
    write_file(lxc.path.join(file), contents)
  end
  HOSTNAME_FILES.each do |file|
    next unless File.exists?(lxc.path.join(file))
    next if args.include?("no_#{file.split('/').last}".to_sym)
    contents = File.read(lxc.path.join(file)).gsub(original, name)
    write_file(lxc.path.join(file), contents)
  end
  true
end

#update_net_hwaddrTrueClass

Update network hardware address

Returns:

  • (TrueClass)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/elecksee/helpers/copies.rb', line 37

def update_net_hwaddr
  contents = File.readlines(lxc.config).map do |line|
    if(line.start_with?('lxc.network.hwaddr'))
      parts = line.split('=')
      "#{parts.first.strip} = 00:16:3e#{SecureRandom.hex(3).gsub(/(..)/, ':\1')}\n"
    else
      line
    end
  end.join
  write_file(lxc.config, contents)
  true
end

#update_rootfs(rootfs_path) ⇒ TrueClass

Update the rootfs

Parameters:

  • rootfs_path (String)

    new rootfs path

Returns:

  • (TrueClass)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/elecksee/helpers/copies.rb', line 22

def update_rootfs(rootfs_path)
  contents = File.readlines(lxc.config.to_s).map do |line|
    if(line.start_with?('lxc.rootfs'))
      "lxc.rootfs = #{rootfs_path}\n"
    else
      line
    end
  end.join
  write_file(lxc.config, contents)
  true
end

#write_file(path, contents) ⇒ TrueClass

Write file

Parameters:

  • path (String)
  • contents (String, Array<String>)

Returns:

  • (TrueClass)


55
56
57
58
59
60
61
62
63
64
# File 'lib/elecksee/helpers/copies.rb', line 55

def write_file(path, contents)
  contents = contents.join if contents.is_a?(Array)
  tmp = Tempfile.new('lxc-copy')
  tmp.write(contents)
  tmp.close
  command("cp #{tmp.path} #{path}", :sudo => true)
  tmp.unlink
  command("chmod 0644 #{path}", :sudo => true)
  true
end