From 81f91f03b41e3c2e3bd875f7a480337cb5109b7f Mon Sep 17 00:00:00 2001 From: Jiang Zhu Date: Sun, 21 Aug 2022 16:22:15 +0800 Subject: [PATCH] add env var to specify config location --- cmd/headscale/cli/root.go | 3 ++ integration_cli_test.go | 38 +++++++++++++++ .../etc/alt-env-config.dump.gold.yaml | 47 +++++++++++++++++++ integration_test/etc/alt-env-config.yaml | 25 ++++++++++ 4 files changed, 113 insertions(+) create mode 100644 integration_test/etc/alt-env-config.dump.gold.yaml create mode 100644 integration_test/etc/alt-env-config.yaml diff --git a/cmd/headscale/cli/root.go b/cmd/headscale/cli/root.go index 2c28c584..459a99fd 100644 --- a/cmd/headscale/cli/root.go +++ b/cmd/headscale/cli/root.go @@ -25,6 +25,9 @@ func init() { } func initConfig() { + if cfgFile == "" { + cfgFile = os.Getenv("HEADSCALE_CONFIG") + } if cfgFile != "" { err := headscale.LoadConfig(cfgFile, true) if err != nil { diff --git a/integration_cli_test.go b/integration_cli_test.go index f2066021..d2e28bee 100644 --- a/integration_cli_test.go +++ b/integration_cli_test.go @@ -1739,6 +1739,8 @@ func (s *IntegrationCLITestSuite) TestLoadConfigFromCommand() { assert.Nil(s.T(), err) altConfig, err := os.ReadFile("integration_test/etc/alt-config.dump.gold.yaml") assert.Nil(s.T(), err) + altEnvConfig, err := os.ReadFile("integration_test/etc/alt-env-config.dump.gold.yaml") + assert.Nil(s.T(), err) _, err = ExecuteCommand( &s.headscale, @@ -1771,4 +1773,40 @@ func (s *IntegrationCLITestSuite) TestLoadConfigFromCommand() { assert.Nil(s.T(), err) assert.YAMLEq(s.T(), string(altConfig), string(altDumpConfig)) + + _, err = ExecuteCommand( + &s.headscale, + []string{ + "headscale", + "dumpConfig", + }, + []string{ + "HEADSCALE_CONFIG=/etc/headscale/alt-env-config.yaml", + }, + ) + assert.Nil(s.T(), err) + + altEnvDumpConfig, err := os.ReadFile("integration_test/etc/config.dump.yaml") + assert.Nil(s.T(), err) + + assert.YAMLEq(s.T(), string(altEnvConfig), string(altEnvDumpConfig)) + + _, err = ExecuteCommand( + &s.headscale, + []string{ + "headscale", + "-c", + "/etc/headscale/alt-config.yaml", + "dumpConfig", + }, + []string{ + "HEADSCALE_CONFIG=/etc/headscale/alt-env-config.yaml", + }, + ) + assert.Nil(s.T(), err) + + altDumpConfig, err = os.ReadFile("integration_test/etc/config.dump.yaml") + assert.Nil(s.T(), err) + + assert.YAMLEq(s.T(), string(altConfig), string(altDumpConfig)) } diff --git a/integration_test/etc/alt-env-config.dump.gold.yaml b/integration_test/etc/alt-env-config.dump.gold.yaml new file mode 100644 index 00000000..2c10db93 --- /dev/null +++ b/integration_test/etc/alt-env-config.dump.gold.yaml @@ -0,0 +1,47 @@ +acl_policy_path: "" +cli: + insecure: false + timeout: 5s +db_path: /tmp/integration_test_db.sqlite3 +db_type: sqlite3 +derp: + auto_update_enabled: false + server: + enabled: false + stun: + enabled: true + update_frequency: 1m + urls: + - https://controlplane.tailscale.com/derpmap/default +dns_config: + base_domain: headscale.net + domains: [] + magic_dns: true + nameservers: + - 1.1.1.1 +ephemeral_node_inactivity_timeout: 30m +node_update_check_interval: 100s +grpc_allow_insecure: false +grpc_listen_addr: :50443 +ip_prefixes: + - fd7a:115c:a1e0::/48 + - 100.64.0.0/10 +listen_addr: 0.0.0.0:18080 +log_level: disabled +logtail: + enabled: false +metrics_listen_addr: 127.0.0.1:19090 +oidc: + scope: + - openid + - profile + - email + strip_email_domain: true +private_key_path: private.key +server_url: http://headscale:18080 +tls_client_auth_mode: relaxed +tls_letsencrypt_cache_dir: /var/www/.cache +tls_letsencrypt_challenge_type: HTTP-01 +unix_socket: /var/run/headscale.sock +unix_socket_permission: "0o770" +randomize_client_port: false diff --git a/integration_test/etc/alt-env-config.yaml b/integration_test/etc/alt-env-config.yaml new file mode 100644 index 00000000..2dc3024f --- /dev/null +++ b/integration_test/etc/alt-env-config.yaml @@ -0,0 +1,25 @@ +log_level: trace +acl_policy_path: "" +db_type: sqlite3 +ephemeral_node_inactivity_timeout: 30m +node_update_check_interval: 100s +ip_prefixes: + - fd7a:115c:a1e0::/48 + - 100.64.0.0/10 +dns_config: + base_domain: headscale.net + magic_dns: true + domains: [] + nameservers: + - 1.1.1.1 +db_path: /tmp/integration_test_db.sqlite3 +private_key_path: private.key +listen_addr: 0.0.0.0:18080 +metrics_listen_addr: 127.0.0.1:19090 +server_url: http://headscale:18080 + +derp: + urls: + - https://controlplane.tailscale.com/derpmap/default + auto_update_enabled: false + update_frequency: 1m