-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshark
executable file
·125 lines (98 loc) · 3.85 KB
/
shark
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
#!/usr/bin/env bash
set -o errexit -o errtrace -o nounset -o pipefail
# Global variable names begin with double underscore.
__project_path="$PWD"
__vm_config_file_name='Sharkfile'
__vm_config_file_path="${__project_path}/${__vm_config_file_name}"
error_message_and_exit() {
set_text_color 'red'
echo "Error: ${1}" >&2
set_text_color 'restore'
exit 1
}
info_message() {
set_text_color 'blue'
echo "Info: ${1}"
set_text_color 'restore'
}
set_text_color() {
if [[ 'red' == ${1} ]]; then
echo -e "\\x1b[31m" 1>&2
elif [[ 'blue' == ${1} ]]; then
echo -e "\\x1b[34m" 1>&2
elif [[ 'restore' == ${1} ]]; then
echo -e "\\x1b[0m" 1>&2
else
echo "Text color '${1}' is not supported." >&2
exit 1
fi
}
__action=''
[[ ! -z ${1:-} ]] && __action="${1}"
[[ '' = ${__action} ]] && error_message_and_exit "Action must be defined (first argument). Try using 'init' action which will create config files for VM in the current directory."
init() {
[[ -f ${__vm_config_file_path} ]] \
&& error_message_and_exit "File '${__vm_config_file_path}' already exists."
cat <<- 'EOF' > "${__vm_config_file_path}"
#!/usr/bin/env bash
# These variables MUST be defined:
__original_cloud_image_path=''
# VM name should be unique across all VMs on the current host.
__vm_name=''
# Leave default values if you wish:
__vm_disk_image_directory="${HOME}/.cache/${__vm_config_file_name}/${__vm_name}"
__vm_disk_image_file_path="${__vm_disk_image_directory}/${__vm_name}.qcow2"
__vm_memory=1024
__vm_vcpus=1
__vm_disk="'${__vm_disk_image_file_path}',size=20"
__vm_cloud_init='user-data=user-data'
__virt_install_command="virt-install --import --osinfo detect=on,require=off --name '${__vm_name}' --memory '${__vm_memory}' --vcpus '${__vm_vcpus}' --disk '${__vm_disk}' --cloud-init '${__vm_cloud_init}'"
# Before creating VM, for the first time, with `up` action to see it in Virtual Machine Manager in
# `File > Add Connection... > Hypervisor` select `QEMU/KVM user session` which is on URI:
# qemu:///session
EOF
[[ -f './user-data' ]] \
&& error_message_and_exit "File '${__project_path}/user-data' already exists."
cat <<- 'EOF' > './user-data'
#cloud-config
system_info:
default_user:
name: "admin"
plain_text_passwd: "admin"
lock_passwd: false
groups: ["sudo"]
EOF
info_message "Initialization is done. Edit '${__vm_config_file_path}' before starting VM with 'up' action."
}
load_and_verify_vm_config() {
[[ ! -f ${__vm_config_file_path} ]] \
&& error_message_and_exit "File '${__vm_config_file_path}' is not found. Use 'init' action to create one."
source "${__vm_config_file_path}"
[[ ! -f ${__original_cloud_image_path} ]] \
&& error_message_and_exit "File '${__original_cloud_image_path}' is not found, it is defined as original cloud image in '${__vm_config_file_path}'."
[[ -z ${__vm_name} ]] \
&& error_message_and_exit "Name for VM is not defined in '${__vm_config_file_path}'."
info_message "VM configuration is valid."
}
up() {
load_and_verify_vm_config
mkdir -p "${__vm_disk_image_directory}"
if [[ -f ${__vm_disk_image_file_path} ]]; then
info_message "VM disk image already exists '${__vm_disk_image_file_path}', we are not going to copy distribution cloud image."
else
info_message "Copy distribution cloud image '${__original_cloud_image_path}' to be used as disk image for VM '${__vm_disk_image_file_path}'."
cp "${__original_cloud_image_path}" "${__vm_disk_image_file_path}"
fi
eval "${__virt_install_command}"
}
main() {
if [[ 'init' == ${__action} ]]; then
init
elif [[ 'up' == ${__action} ]]; then
up
else
error_message_and_exit "Action '${__action}' is not supported."
fi
}
main "$@"
exit