-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerFilePath.pm
executable file
·116 lines (91 loc) · 2.22 KB
/
DockerFilePath.pm
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
package DockerFilePath;
our $basefile = "DockerFilePath.pm";
our $logfile = 'general.log';
our $timestamp = qx(date '+%Y-%m-%d %H:%M:%S');
our %platform = ("-u -c" => "/centos",
"-u -u" => "/ubuntu");
our %action = ("-u" => ".",
"-d" => "down",
"-h" => "help");
sub startup {
my($path, $fh) = @_;
my $lfh = $$fh;
my $scope="startup";
my $docker_img_name="python-cli-whereami";
my $info_base="[$timestamp INFO]: $basefile::$scope";
print($lfh "$info_base started\n");
print($lfh "$info_base build image from $path\n");
print($lfh qx(sudo docker build -t $docker_img_name $path));
print($lfh "$info_base running image\n");
print($lfh qx(sudo docker run -ti --rm $docker_img_name));
print($lfh "$info_base ended\n");
}
sub teardown {
my $fh = shift;
my $lfh = $$fh;
my $scope="teardown";
my $info_base="[$timestamp INFO]: $basefile::$scope";
print($lfh "$info_base started\n");
print($lfh "$info_base services removed\n");
print($lfh "$info_base ended\n");
}
sub usage {
$msg = "Usage\n";
$msg .= "-u <PLATFORM> start\n";
$msg .= "PLATFORM\n";
$msg .= "<blank> alpine\n";
$msg .= "-c centos\n";
$msg .= "-u ubuntu\n";
return $msg;
}
sub hash_sub {
my ($opt, $hash_ref) = @_;
my %hash = %{ $hash_ref };
if(defined $hash{$opt}){
return $hash{$opt};
} else {
print "key: $opt not found.\n";
print &usage;
exit 1;
}
}
sub opt {
my ($opt, $hash_ref) = @_;
chomp($opt);
return &hash_sub($opt, $hash_ref);
}
sub fire {
my $line = shift;
open my $fh, '>./general.log' or die "";
if( (rindex $line, ".", 0) == 0 )
{
&startup($line, \$fh);
}
elsif ( $line eq "down" ){
&teardown(\$fh);
}
else{
print &usage;
}
close $fh;
print(qx(grep OUTPUT general.log));
}
sub create{
my $arr_ref = shift;
my @arr = @{ $arr_ref };
my $line = "";
my $prior = "";
for ( my $i = 0; $i < $#arr+1; $i++ ){
if($i == 0) {
$prior = $arr[$i];
$line .= &opt($arr[$i], \%action);
}
elsif($i == 1) {
$prior .= " " . $arr[$i];
$line .= &opt($prior, \%platform);
}
}
#print rindex $line, ".", 0 . "\n";
&fire($line);
}
1;