FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
zstack/utils/src/main/java/org/zstack/utils/Bash.java at master · zstackio/zstack · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
zstackio
/
zstack
Public
Notifications
You must be signed in to change notification settings
Fork
399
Star
1.4k
Code
Issues
163
Pull requests
13
Actions
Projects
Wiki
Security and quality
4
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
zstack
/
utils
/
src
/
main
/
java
/
org
/
zstack
/
utils
/
Bash.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
156 lines (127 loc) · 4 KB
Breadcrumbs
zstack
/
utils
/
src
/
main
/
java
/
org
/
zstack
/
utils
/
Bash.java
Copy path
File metadata and controls
executable file
·
156 lines (127 loc) · 4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package
org
.
zstack
.
utils
;
import
org
.
apache
.
commons
.
io
.
FileUtils
;
import
org
.
zstack
.
utils
.
logging
.
CLogger
;
import
java
.
io
.
File
;
import
java
.
io
.
IOException
;
public
abstract
class
Bash
{
private
static
final
CLogger
logger
=
Utils
.
getLogger
(
Bash
.
class
);
protected
abstract
void
scripts
();
protected
int
lastReturnCode
;
protected
String
lastStdout
;
protected
String
lastStderr
;
protected
String
lastCommand
;
protected
boolean
SET_E
;
protected
class
BashBuilder
{
private
boolean
useSudo
;
private
String
path
;
public
BashBuilder
sudo
() {
useSudo
=
true
;
return
this
;
}
public
BashBuilder
cwd
(
String
v
) {
path
=
v
;
return
this
;
}
public
int
run
(
String
cmd
,
Object
...
args
) {
if
(
args
!=
null
) {
cmd
=
String
.
format
(
cmd
,
args
);
}
lastCommand
=
Utils
.
maskSensitiveInfo
(
cmd
);
ShellResult
res
=
ShellUtils
.
runAndReturn
(
cmd
,
path
,
useSudo
);
lastReturnCode
=
res
.
getRetCode
();
lastStdout
=
res
.
getStdout
();
lastStderr
=
res
.
getStderr
();
if
(
SET_E
) {
errorOnFailure
();
}
return
lastReturnCode
;
}
}
/**
* @return return true if @content is equal to @filePath's content
*/
protected
boolean
compareWithFile
(
String
filePath
,
String
content
) {
File
f
=
new
File
(
filePath
);
if
(!
f
.
exists
() ||
f
.
isDirectory
()) {
return
false
;
}
try
{
String
fileContent
=
FileUtils
.
readFileToString
(
f
);
return
fileContent
.
trim
().
equals
(
content
.
trim
());
}
catch
(
IOException
e
) {
logger
.
warn
(
e
.
getLocalizedMessage
());
return
false
;
}
}
protected
void
writeFile
(
String
filePath
,
String
content
) {
mkdirs
(
dirname
(
filePath
));
try
{
FileUtils
.
writeStringToFile
(
new
File
(
filePath
),
content
);
}
catch
(
IOException
e
) {
throw
new
RuntimeException
(
e
);
}
}
protected
void
mkdirs
(
String
path
) {
File
f
=
new
File
(
path
);
if
(!
f
.
exists
()) {
f
.
mkdirs
();
}
}
protected
String
dirname
(
String
path
) {
return
new
File
(
path
).
getParentFile
().
getPath
();
}
protected
void
copyDir
(
String
src
,
String
dst
) {
try
{
FileUtils
.
copyDirectory
(
new
File
(
src
),
new
File
(
dst
));
}
catch
(
IOException
e
) {
throw
new
RuntimeException
(
e
);
}
}
protected
BashBuilder
bash
() {
return
new
BashBuilder
();
}
protected
int
run
(
String
cmd
,
Object
...
args
) {
return
run
(
cmd
,
true
,
args
);
}
protected
int
run
(
String
cmd
,
boolean
sudo
,
Object
...
args
) {
if
(
args
!=
null
) {
cmd
=
String
.
format
(
cmd
,
args
);
}
lastCommand
=
cmd
;
ShellResult
res
=
ShellUtils
.
runAndReturn
(
cmd
,
sudo
);
lastReturnCode
=
res
.
getRetCode
();
lastStdout
=
res
.
getStdout
();
lastStderr
=
res
.
getStderr
();
if
(
SET_E
) {
errorOnFailure
();
}
return
lastReturnCode
;
}
protected
void
errorOnFailure
() {
if
(
lastReturnCode
!=
0
) {
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
String
.
format
(
"
\n
shell command[%s] failed"
,
lastCommand
));
sb
.
append
(
String
.
format
(
"
\n
ret code: %s"
,
lastReturnCode
));
sb
.
append
(
String
.
format
(
"
\n
stderr: %s"
,
lastStderr
));
sb
.
append
(
String
.
format
(
"
\n
stdout: %s"
,
lastStdout
));
throw
new
RuntimeException
(
sb
.
toString
());
}
}
// similar to set -e in shell script
protected
void
setE
() {
SET_E
=
true
;
}
// similar to set +e in shell script
protected
void
unsetE
() {
SET_E
=
false
;
}
protected
String
stdout
() {
return
lastStdout
;
}
protected
String
stderr
() {
return
lastStderr
;
}
public
void
execute
() {
scripts
();
}
}
Back
|
FazBrowse Home
|
New Git URL