@@ -5,12 +5,16 @@ use nebulous::create_app_state;
55use nebulous:: proxy:: server:: start_proxy;
66use nebulous:: resources:: v1:: containers:: controller:: ContainerController ;
77use nebulous:: resources:: v1:: processors:: controller:: ProcessorController ;
8+ use serde_json:: Value ;
89use std:: error:: Error ;
10+ use std:: io:: Write ;
11+ use std:: ops:: Add ;
12+ use std:: process:: { Command , Stdio } ;
913
10- pub async fn execute (
14+ pub async fn launch_server (
1115 host : String ,
1216 port : u16 ,
13- internal_auth : bool ,
17+ disable_internal_auth : bool ,
1418 auth_port : u16 ,
1519) -> Result < ( ) , Box < dyn Error > > {
1620 let app_state = create_app_state ( ) . await ?;
@@ -37,7 +41,7 @@ pub async fn execute(
3741 } ) ;
3842 println ! ( "Proxy server started in background" ) ;
3943
40- if internal_auth {
44+ if !disable_internal_auth {
4145 println ! ( "Starting auth server" ) ;
4246 tokio:: spawn ( {
4347 let auth_state = app_state. clone ( ) ;
@@ -63,3 +67,71 @@ pub async fn execute(
6367
6468 Ok ( ( ) )
6569}
70+
71+ const BASE_COMPOSE : & str = include_str ! ( "../../deploy/docker/docker-compose.yml" ) ;
72+
73+ pub async fn launch_docker (
74+ port : u16 ,
75+ disable_internal_auth : bool ,
76+ auth_port : u16 ,
77+ ) -> Result < ( ) , Box < dyn Error > > {
78+ Command :: new ( "docker" )
79+ . args ( & [ "compose" , "version" ] )
80+ . output ( )
81+ . expect (
82+ "Did not find docker compose. Please ensure that docker is installed and in your PATH." ,
83+ ) ;
84+
85+ // Ask the user for the tailscale login server and auth key
86+ let tailscale_login_server = std:: env:: var ( "TS_LOGIN_SERVER" ) . unwrap_or_else ( |_| {
87+ println ! ( "Please enter the Tailscale login server URL (or leave blank for default):" ) ;
88+ let mut input = String :: new ( ) ;
89+ std:: io:: stdin ( ) . read_line ( & mut input) . unwrap ( ) ;
90+ input. trim ( ) . to_string ( )
91+ } ) ;
92+ let tailscale_auth_key = std:: env:: var ( "TS_AUTH_KEY" ) . unwrap_or_else ( |_| {
93+ println ! ( "Please enter the Tailscale auth key:" ) ;
94+ let mut input = String :: new ( ) ;
95+ std:: io:: stdin ( ) . read_line ( & mut input) . unwrap ( ) ;
96+ input. trim ( ) . to_string ( )
97+ } ) ;
98+
99+ let mut doc: Value = serde_yaml:: from_str ( BASE_COMPOSE ) ?;
100+
101+ doc[ "services" ] [ "tailscale" ] [ "environment" ] [ 2 ] =
102+ Value :: String ( format ! ( "TS_AUTH_KEY={}" , tailscale_auth_key) ) ;
103+ if !tailscale_login_server. is_empty ( ) {
104+ doc[ "services" ] [ "tailscale" ] [ "environment" ] [ 3 ] = Value :: String ( format ! (
105+ "TS_EXTRA_ARGS=--login-server {}" ,
106+ tailscale_login_server
107+ ) ) ;
108+ }
109+
110+ let mut command = format ! (
111+ "exec nebu serve --host 0.0.0.0 --port {} --auth-port {}" ,
112+ port, auth_port
113+ ) ;
114+ if disable_internal_auth {
115+ command = command. add ( " --disable-internal-auth" ) ;
116+ } ;
117+ doc[ "services" ] [ "nebulous" ] [ "command" ] [ 2 ] = Value :: String ( command) ;
118+
119+ let yaml = serde_yaml:: to_string ( & doc) ?;
120+
121+ let mut child = Command :: new ( "docker" )
122+ . args ( & [ "compose" , "-f" , "-" , "up" ] )
123+ . stdin ( Stdio :: piped ( ) )
124+ . spawn ( ) ?;
125+
126+ {
127+ let stdin = child. stdin . as_mut ( ) . expect ( "Failed to open stdin" ) ;
128+ stdin. write_all ( yaml. as_bytes ( ) ) ?;
129+ }
130+
131+ let status = child. wait ( ) ?;
132+ if !status. success ( ) {
133+ Err ( "docker compose failed" . into ( ) )
134+ } else {
135+ Ok ( ( ) )
136+ }
137+ }
0 commit comments