Skip to content

Commit d0e55df

Browse files
committed
initial commit
0 parents  commit d0e55df

21 files changed

+520
-0
lines changed

.gitattributes

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This is a good default: files that are auto-detected by git to be text are
2+
# converted to the platform-native line ending (LF on Unix, CRLF on Windows)
3+
# in the working tree and to LF in the repository.
4+
#
5+
* text=auto
6+
7+
# Use `eol=crlf` for files that should have the CRLF line ending both in the
8+
# working tree (even on Unix) and in the repository.
9+
#
10+
#*.bat text eol=crlf
11+
12+
# Use `eol=lf` for files that should have the LF line ending both in the
13+
# working tree (even on Windows) and in the repository.
14+
#
15+
#*.sh text eol=lf
16+
17+
# Use `binary` to make sure certain files are never auto-detected as text.
18+
#
19+
#*.png binary

.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.bdep/
2+
3+
# Local default options files.
4+
#
5+
.build2/local/
6+
7+
# Compiler/linker output.
8+
#
9+
*.d
10+
*.t
11+
*.i
12+
*.i.*
13+
*.ii
14+
*.ii.*
15+
*.o
16+
*.obj
17+
*.gcm
18+
*.pcm
19+
*.ifc
20+
*.so
21+
*.dll
22+
*.a
23+
*.lib
24+
*.exp
25+
*.pdb
26+
*.ilk
27+
*.exe
28+
*.exe.dlls/
29+
*.exe.manifest
30+
*.pc

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# crails-deploy
2+
3+
C++ executable

build/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/config.build
2+
/root/
3+
/bootstrap/
4+
build/

build/bootstrap.build

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
project = crails-deploy
2+
3+
using version
4+
using config
5+
using test
6+
using install
7+
using dist

build/root.build

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Uncomment to suppress warnings coming from external libraries.
2+
#
3+
#cxx.internal.scope = current
4+
5+
cxx.std = latest
6+
7+
using cxx
8+
9+
hxx{*}: extension = hpp
10+
ixx{*}: extension = ixx
11+
txx{*}: extension = txx
12+
cxx{*}: extension = cpp
13+
14+
# Assume headers are importable unless stated otherwise.
15+
#
16+
hxx{*}: cxx.importable = true
17+
18+
# The test target for cross-testing (running tests under Wine, etc).
19+
#
20+
test.target = $cxx.target

buildfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./: {*/ -build/} doc{README.md} manifest

crails-deploy/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
crails-deploy
2+
3+
# Testscript output directory (can be symlink).
4+
#
5+
test-crails-deploy

crails-deploy/buildfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
libs =
2+
import libs += libcrails-cli%lib{crails-cli}
3+
import libs += libcrails-ssh%lib{crails-ssh}
4+
import libs += libcrails-logger%lib{crails-logger}
5+
6+
exe{crails-deploy}: {hxx ixx txx cxx}{**} $libs testscript
7+
8+
cxx.poptions =+ "-I$out_root" "-I$src_root"
9+
cxx.loptions =+ "-Wl,--start-group" -lssh -lcrypto -lssl -lboost_program_options

crails-deploy/deploy_application.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "deploy_application.hpp"
2+
3+
using namespace Crails;
4+
using namespace std;
5+
6+
void ApplicationPackageDeploy::install_application()
7+
{
8+
deploy_package();
9+
create_runtime_directory();
10+
create_log_directory();
11+
}
12+
13+
void ApplicationPackageDeploy::deploy_package()
14+
{
15+
stringstream command;
16+
int rc;
17+
18+
if (sudo) command << "sudo ";
19+
command << "tar xf \"" << tmp_filepath << "\" --directory /";
20+
ssh.make_scp_session("/tmp", Ssh::WriteMode)->push_file(package, tmp_filename);
21+
rc = ssh.exec(command.str(), std_out);
22+
if (rc != 0)
23+
throw std::runtime_error("could not extract package");
24+
ssh.exec("rm \"" + tmp_filepath + '"', null_output);
25+
}
26+
27+
void ApplicationPackageDeploy::create_runtime_directory()
28+
{
29+
if (mkdir(runtime_directory) != 0)
30+
throw std::runtime_error("could not create runtime directory");
31+
}
32+
33+
void ApplicationPackageDeploy::create_log_directory()
34+
{
35+
if (mkdir(log_directory) != 0)
36+
throw std::runtime_error("could not create log directory");
37+
}
38+
39+
int ApplicationPackageDeploy::mkdir(const std::string& path)
40+
{
41+
stringstream command;
42+
43+
if (sudo) command << "sudo ";
44+
command << "mkdir -p \"" << path << '"';
45+
return ssh.exec(command.str(), null_output);
46+
}

crails-deploy/deploy_application.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include "settings.hpp"
3+
#include "ssh.hpp"
4+
5+
class ApplicationPackageDeploy :
6+
protected virtual ApplicationDeploySettings,
7+
public virtual ApplicationDeploySsh
8+
{
9+
public:
10+
void install_application();
11+
12+
private:
13+
void deploy_package();
14+
void create_runtime_directory();
15+
void create_log_directory();
16+
17+
int mkdir(const std::string& path);
18+
};

crails-deploy/deploy_permissions.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "deploy_permissions.hpp"
2+
3+
using namespace std;
4+
using namespace Crails;
5+
6+
void ApplicationPermissionDeploy::set_permissions()
7+
{
8+
set_permissions_on(
9+
runtime_directory,
10+
"gu+rw,o-rwx"
11+
);
12+
set_permissions_on(
13+
log_directory,
14+
"gu+rw,o-rwx"
15+
);
16+
set_permissions_on(
17+
root + "/share/" + app_name,
18+
"gu+r,o-rwx"
19+
);
20+
set_permissions_on(
21+
root + "/bin/" + app_name,
22+
"gu+rx,o-rwx"
23+
);
24+
}
25+
26+
void ApplicationPermissionDeploy::set_permissions_on(const std::string& target, const std::string permissions)
27+
{
28+
int rc =
29+
chown(target) +
30+
chgrp(target) +
31+
chmod(target, permissions);
32+
if (rc != 0)
33+
throw std::runtime_error("could not set permissions");
34+
}
35+
36+
int ApplicationPermissionDeploy::chown(const std::string& target)
37+
{
38+
stringstream command;
39+
40+
if (sudo) command << "sudo ";
41+
command << "chown -R \"" << app_user << "\" \"" << target << '"';
42+
return ssh.exec(command.str(), null_output);
43+
}
44+
45+
int ApplicationPermissionDeploy::chgrp(const std::string& target)
46+
{
47+
stringstream command;
48+
49+
if (sudo) command << "sudo ";
50+
command << "chgrp -R \"" << app_group << "\" \"" << target << '"';
51+
return ssh.exec(command.str(), null_output);
52+
}
53+
54+
int ApplicationPermissionDeploy::chmod(const std::string& target, const std::string& permissions)
55+
{
56+
stringstream command;
57+
58+
if (sudo) command << "sudo ";
59+
command << "chmod -R " << permissions << " \"" << target << '"';
60+
return ssh.exec(command.str(), null_output);
61+
}

crails-deploy/deploy_permissions.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include "settings.hpp"
3+
#include "ssh.hpp"
4+
5+
class ApplicationPermissionDeploy :
6+
protected virtual ApplicationDeploySettings,
7+
protected virtual ApplicationDeploySsh
8+
{
9+
public:
10+
void set_permissions();
11+
12+
private:
13+
void set_permissions_on(const std::string& target, const std::string permissions);
14+
15+
int chown(const std::string& target);
16+
int chgrp(const std::string& target);
17+
int chmod(const std::string& target, const std::string& permissions);
18+
};

crails-deploy/deploy_user.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "deploy_user.hpp"
2+
3+
using namespace Crails;
4+
using namespace std;
5+
6+
void ApplicationUserDeploy::prepare_application_user()
7+
{
8+
if (!group_exists())
9+
create_group();
10+
if (!user_exists())
11+
create_user();
12+
else
13+
assign_group();
14+
}
15+
16+
bool ApplicationUserDeploy::user_exists()
17+
{
18+
return ssh.exec("id -u \"" + app_user + '"', null_output) == 0;
19+
}
20+
21+
bool ApplicationUserDeploy::group_exists()
22+
{
23+
return ssh.exec("grep \"^" + app_group + ":\" /etc/group", null_output) == 0;
24+
}
25+
26+
void ApplicationUserDeploy::create_user()
27+
{
28+
stringstream command;
29+
int rc;
30+
31+
if (sudo) command << "sudo ";
32+
command << "useradd --no-create-home \"" << app_user << '"'
33+
<< " -g \"" << app_group << '"';
34+
rc = ssh.exec(command.str(), null_output);
35+
}
36+
37+
void ApplicationUserDeploy::create_group()
38+
{
39+
stringstream command;
40+
int rc;
41+
42+
if (sudo) command << "sudo ";
43+
command << "groupadd \"" << app_group << '"';
44+
rc = ssh.exec(command.str(), null_output);
45+
if (rc != 0)
46+
throw std::runtime_error("could not create app group");
47+
}
48+
49+
void ApplicationUserDeploy::assign_group()
50+
{
51+
stringstream command;
52+
int rc;
53+
54+
if (sudo) command << "sudo ";
55+
command << "usermod -g \"" << app_group << '"'
56+
<< " \"" << app_user << '"';
57+
rc = ssh.exec(command.str(), null_output);
58+
if (rc != 0)
59+
throw std::runtime_error("could not assign group to user");
60+
}

crails-deploy/deploy_user.hpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
#include "settings.hpp"
3+
#include "ssh.hpp"
4+
5+
class ApplicationUserDeploy :
6+
protected virtual ApplicationDeploySettings,
7+
protected virtual ApplicationDeploySsh
8+
{
9+
public:
10+
void prepare_application_user();
11+
12+
private:
13+
bool user_exists();
14+
bool group_exists();
15+
void create_user();
16+
void create_group();
17+
void assign_group();
18+
};

0 commit comments

Comments
 (0)