1 /*************************************************************************
3 * Copyright 2021 highstreet technologies GmbH and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include "utils/nts_utils.h"
27 #include "core/framework.h"
28 #include "core/container.h"
29 #include "core/session.h"
30 #include "core/nc_config.h"
32 #define DOCKER_DEPLOY_ZIP "/opt/dev/deploy.zip"
33 #define FTP_DEPLOY_ZIP "/ftp/deploy.zip"
35 //checkAL: see all todos if doing also YANG-install through netopeer
38 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"running as BLANK NTS daemon...\n"LOG_COLOR_RESET);
39 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"Docker IP:"LOG_COLOR_RESET" %s\n", framework_environment.settings.ip_v6_enabled ? framework_environment.settings.ip_v6 : framework_environment.settings.ip_v4);
41 char shell_command[512];
43 if(session_init() != NTS_ERR_OK) {
44 log_error("session_init() failed\n");
45 return NTS_ERR_FAILED;
48 if(netconf_configure() != NTS_ERR_OK) {
49 log_error("netconf_configure() failed\n")
50 return NTS_ERR_FAILED;
60 char *zipfile = DOCKER_DEPLOY_ZIP;
61 if(file_exists(zipfile)) {
62 goto blank_install_zip;
65 log_add_verbose(2, "%s was not found, waiting for deploy.zip on other means...\n");
68 blank_run_wait_for_file:
69 while(!framework_sigint) {
70 zipfile = FTP_DEPLOY_ZIP;
71 if(file_exists(zipfile)) {
72 goto blank_install_zip;
75 //todo: check netopeer for data
80 vsftp_daemon_deinit();
83 log_error("blank image daemon was called to stop before installing anything\n");
84 return NTS_ERR_FAILED;
88 log_add_verbose(1, "found deploy.zip in "LOG_COLOR_BOLD_BLUE"%s"LOG_COLOR_RESET"\n", zipfile);
89 log_add_verbose(1, "starting install...\n");
92 sprintf(shell_command, "unzip -qq -t %s", zipfile);
93 if(system(shell_command) != 0) {
94 log_error("%s invalid ZIP file\n", zipfile);
95 goto blank_install_failed;
98 //unzip to /opt/dev/deploy
99 sprintf(shell_command, "unzip -qq %s -d /opt/dev/deploy", zipfile);
100 if(system(shell_command) != 0) {
101 log_error("unzip filed for unknown reason\n", zipfile);
102 goto blank_install_failed;
105 if(strcmp(zipfile, FTP_DEPLOY_ZIP) == 0) {
106 sprintf(shell_command, "rm -f %s", zipfile);
107 if(system(shell_command) != 0) {
108 log_error("failed to remove %s\n", zipfile);
112 if(!file_exists("/opt/dev/deploy/config.json")) {
113 log_error("/opt/dev/deploy/config.json not found!\n");
114 goto blank_install_failed;
117 //move /opt/dev/deploy/config.json to /opt/dev/ntsim-ng/config/config.json
118 system("mv /opt/dev/deploy/config.json /opt/dev/ntsim-ng/config/config.json");
120 //todo: kill netopeer
122 //run container_self_init()
123 if(!container_self_init()) {
124 log_error("container_self_init() error\n");
125 goto blank_install_failed;
128 //send SIGUSR1 to supervisor to reload everything and start fresh
131 log_add_verbose(1, LOG_COLOR_BOLD_GREEN"blank image successfully replaced!"LOG_COLOR_RESET"\n");
132 vsftp_daemon_deinit();
133 sftp_daemon_deinit();
138 blank_install_failed: {
140 sprintf(shell_command, "rm -rf %s", zipfile);
141 system(shell_command);
143 system("rm -rf /opt/dev/deploy");
145 log_error("%s failed to install...\n", zipfile);
146 log_error("try again with new file...\n");
147 goto blank_run_wait_for_file;