Ejemplo n.º 1
0
func TestUserLookup(t *testing.T) {
	if os.Geteuid() != 0 {
		t.Skip("test requires root for chroot(), skipping")
	}

	// perform a user lookup to ensure libnss_files.so is loaded
	// note this assumes /etc/nsswitch.conf invokes files.
	user.Lookup("root")

	td, err := tempBase()
	if err != nil {
		t.Fatalf("temp base error: %v", err)
	}

	logger := log.New()
	defer logger.Close()

	u := &Util{
		DestDir: td,
		Logger:  &logger,
	}

	usr, err := u.userLookup("foo")
	if err != nil {
		t.Fatalf("lookup error: %v", err)
	}

	if usr.Name != "foo" {
		t.Fatalf("unexpected name: %q", usr.Name)
	}

	if usr.Uid != "44" {
		t.Fatalf("unexpected uid: %q", usr.Uid)
	}

	if usr.Gid != "4242" {
		t.Fatalf("unexpected gid: %q", usr.Gid)
	}
}
Ejemplo n.º 2
0
func main() {
	flags := struct {
		clearCache   bool
		configCache  string
		fetchTimeout time.Duration
		oem          oem.Name
		providers    providers.List
		root         string
		stage        stages.Name
		version      bool
	}{}

	flag.BoolVar(&flags.clearCache, "clear-cache", false, "clear any cached config")
	flag.StringVar(&flags.configCache, "config-cache", "/tmp/ignition.json", "where to cache the config")
	flag.DurationVar(&flags.fetchTimeout, "fetchtimeout", exec.DefaultFetchTimeout, "")
	flag.Var(&flags.oem, "oem", fmt.Sprintf("current oem. %v", oem.Names()))
	flag.Var(&flags.providers, "provider", fmt.Sprintf("provider of config. can be specified multiple times. %v", providers.Names()))
	flag.StringVar(&flags.root, "root", "/", "root of the filesystem")
	flag.Var(&flags.stage, "stage", fmt.Sprintf("execution stage. %v", stages.Names()))
	flag.BoolVar(&flags.version, "version", false, "print the version and exit")

	flag.Parse()

	if config, ok := oem.Get(flags.oem.String()); ok {
		for k, v := range config.Flags() {
			if err := flag.Set(k, v); err != nil {
				panic(err)
			}
		}
	}

	if flags.version {
		fmt.Printf("ignition %s\n", versionString)
		return
	}

	if flags.stage == "" {
		fmt.Fprint(os.Stderr, "'--stage' must be provided\n")
		os.Exit(2)
	}

	logger := log.New()
	defer logger.Close()

	if flags.clearCache {
		if err := os.Remove(flags.configCache); err != nil {
			logger.Err("unable to clear cache: %v", err)
		}
	}

	engine := exec.Engine{
		Root:         flags.root,
		FetchTimeout: flags.fetchTimeout,
		Logger:       logger,
		ConfigCache:  flags.configCache,
	}.Init()
	for _, name := range flags.providers {
		engine.AddProvider(providers.Get(name).Create(logger))
	}

	if !engine.Run(flags.stage.String()) {
		os.Exit(1)
	}
}