Skip to content

Commit b1e1e8b

Browse files
authored
Merge pull request #155 from d4h0/add-vopono-host-ip-env-variable
Expose $VOPONO_HOST_IP environment variable to the application to run
2 parents 1f9202e + 3c6be87 commit b1e1e8b

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

USERGUIDE.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@ run itself. `$VOPONO_NS_IP` is useful if you'd like to configure a server
5555
running within the network namespace to listen on its local IP address only
5656
(see below, for more information on that).
5757

58+
The application to run within the namespace also has access to
59+
`$VOPONO_HOST_IP`, to get the IP address of the host.
60+
61+
Note: These environment variables are currently only available from within
62+
the application/script to run, not on the command line. So the following
63+
doesn't work:
64+
65+
`vopono exec {other Vopono options} 'echo "HOST IP: $VOPONO_HOST_IP"'`
66+
67+
Output: `HOST IP: $VOPONO_HOST_IP` (the environ variable wasn't expanded).
68+
69+
A work around is to create a executable script, that executes the
70+
application you'd like to run:
71+
72+
```bash
73+
#!/bin/bash
74+
75+
echo "=> NETWORK NAMESPACE IP: $VOPONO_NS_IP"
76+
echo "=> HOST IP: $VOPONO_HOST_IP"
77+
```
78+
79+
Execution: `vopono exec {other Vopono options} '/path/to/the/above/script.sh'`
80+
81+
Output:
82+
83+
```
84+
=> NETWORK NAMESPACE IP: 10.200.1.2
85+
=> HOST IP: 10.200.1.1
86+
```
5887

5988
### Host scripts
6089

@@ -65,6 +94,15 @@ Note these scripts run on the host (outside the network namespace), using the cu
6594
and with the same user as the final application itself (which can be set
6695
with the `user` argument or config file entry).
6796

97+
Script arguments (e.g. `script.sh arg1 arg1`), are currently not possible, resulting in an error:
98+
99+
```
100+
$ vopono exec {other Vopono options} --postup 'echo POSTUP' ls
101+
[...]
102+
sudo: echo POSTUP: command not found
103+
[...]
104+
```
105+
68106
### Wireguard
69107

70108
Install vopono and use `vopono sync` to

src/exec.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,16 @@ pub fn exec(command: ExecCommand) -> anyhow::Result<()> {
392392
}
393393
}
394394

395+
// Set env var referring to the host IP for the application:
396+
std::env::set_var(
397+
"VOPONO_HOST_IP",
398+
&ns.veth_pair_ips.as_ref().unwrap().host_ip.to_string(),
399+
);
400+
395401
let ns = ns.write_lockfile(&command.application)?;
396402

397403
let application = ApplicationWrapper::new(&ns, &command.application, user)?;
398404

399-
std::env::remove_var("VOPONO_NS_IP");
400-
401405
// Launch TCP proxy server on other threads if forwarding ports
402406
// TODO: Fix when running as root
403407
let mut proxy = Vec::new();
@@ -438,6 +442,9 @@ pub fn exec(command: ExecCommand) -> anyhow::Result<()> {
438442
stay_alive(None, signals);
439443
}
440444

445+
std::env::remove_var("VOPONO_NS_IP");
446+
std::env::remove_var("VOPONO_HOST_IP");
447+
441448
Ok(())
442449
}
443450

0 commit comments

Comments
 (0)