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
|
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
package Rex::Virtualization::Docker::create;
use v5.14.4;
use warnings;
our $VERSION = '1.16.1'; # VERSION
use Rex::Logger;
use Rex::Commands::Gather;
use Rex::Hardware;
use Rex::Commands::Fs;
use Rex::Helper::Run;
use Rex::Commands::File;
use Rex::File::Parser::Data;
use Rex::Template;
use XML::Simple;
use Data::Dumper;
sub execute {
my ( $class, $name, %opt ) = @_;
my $opts = \%opt;
$opts->{name} = $name;
unless ($opts) {
die("You have to define the create options!");
}
if ( !exists $opts->{"image"} ) {
die("You have to set a base image.");
}
if ( !exists $opts->{"command"} ) {
$opts->{command} = "";
}
my $options = _format_opts($opts);
my @out = i_run "docker run -d $options $opts->{'image'} $opts->{'command'}";
my $id = pop @out;
return $id;
}
sub _format_opts {
my ($opts) = @_;
# -name=""
# Assign the specified name to the container. If no name is specific docker will generate a random name
if ( !exists $opts->{"name"} ) {
die("You have to give a name.");
}
# -m=""
# Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
if ( !exists $opts->{"memory"} ) {
$opts->{"memory"} = '512m';
}
else {
$opts->{memory} = $opts->{memory};
}
# -c=0
# CPU shares (relative weight)
if ( !exists $opts->{"cpus"} ) {
$opts->{"cpus"} = 0;
}
my $str = "--name $opts->{'name'} -m $opts->{'memory'} -c $opts->{'cpus'} ";
# -e=[]
# Set environment variables
if ( exists $opts->{"env"} ) {
$str .= '-e ' . join( '-e ', @{ $opts->{'env'} } ) . ' ';
}
# -h=""
# Container host name
if ( exists $opts->{"hostname"} ) {
$str .= "-h $opts->{'hostname'} ";
}
# -privileged=false
# Give extended privileges to this container
if ( exists $opts->{"privileged"} ) {
$str .= "--privileged $opts->{'privileged'} ";
}
# -p=[]
# Map a network port to the container
if ( exists $opts->{"forward_port"} ) {
$str .= '-p ' . join( '-p ', @{ $opts->{'forward_port'} } ) . ' ';
}
# -expose=[]
# Expose a port from the container without publishing it to your host
if ( exists $opts->{"expose_port"} ) {
$str .= "--expose $opts->{'expose_port'} ";
}
# -dns=[]
# Set custom dns servers for the container
if ( exists $opts->{"dns"} ) {
$str .= '--dns ' . join( '-dns ', @{ $opts->{'dns'} } ) . ' ';
}
# -v=[]:
# Create a bind mount with: [host-dir]:[container-dir]:[rw|ro].
# If "container-dir" is missing, then docker creates a new volume.
if ( exists $opts->{"share_folder"} ) {
$str .= '-v ' . join( '-v ', @{ $opts->{'share_folder'} } ) . ' ';
}
# -volumes-from=""
# Mount all volumes from the given container(s)
if ( exists $opts->{"volumes-from"} ) {
$str .= "--volumes-from \"$opts->{'volumes-from'}\" ";
}
# -lxc-conf=[]
# Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
if ( exists $opts->{"lxc-conf"} ) {
$str .= "--lxc-conf \"$opts->{'lxc-conf'}\" ";
}
# -link=""
# Add link to another container (name:alias)
if ( exists $opts->{"link"} ) {
$str .= '--link ' . join( '-link ', @{ $opts->{'link'} } ) . ' ';
}
return $str;
}
1;
|