Beispiel #1
0
func executePlan(c *cli.Context) {
	args := c.Args()
	if len(args) == 0 {
		// FIXME: Just print out the usage info?
		log.Fatalf("Missing path to the plan")
	}
	// Step 1: Validate Modules path and see if it exists
	modulesPath := c.String("modules")
	user := c.String("user")
	keyfile := c.String("keyfile")
	_, err := os.Stat(modulesPath)
	if err != nil {
		log.Fatalf("Error when validating the modules directory - %s\n", err.Error())
	}

	// Step 2: Read the inventory
	// FIXME: Support multiple inventory types.
	// We're dealing with YAML for now
	inventoryPath := c.String("inventory")
	inventoryConfig := make(henchman.InventoryConfig)
	inventoryConfig["path"] = inventoryPath
	inventorySource := new(henchman.YAMLInventory)

	tc := make(henchman.TransportConfig)
	tc["username"] = user
	tc["keyfile"] = keyfile

	inv, err := inventorySource.Load(inventoryConfig, tc)
	if err != nil {
		log.Fatalf("Error loading inventory - %s\n", err.Error())
	}

	// Step 3: Read the planFile
	planFile := args[0]
	planBuf, err := ioutil.ReadFile(planFile)
	if err != nil {
		log.Fatalf("Error when reading plan `%s': %s", planFile, err.Error())
	}
	plan, err := henchman.PreprocessPlan(planBuf, inv)
	if err != nil {
		log.Fatalf(err.Error())
	}
	plan.Execute()
	log.Printf("Loaded the plan - %v\n", plan)
}
Beispiel #2
0
func executePlan(c *cli.Context) {
	args := c.Args()
	if len(args) == 0 {
		// FIXME: Just print out the usage info?
		henchman.Fatal(nil, "Missing path to the plan")
	}

	// Step 0: Set global variables and Init stuff
	henchman.DebugFlag = c.Bool("debug")

	// NOTE: can't use HenchErr b/c it hasn't been initialized yet
	if err := henchman.InitConfiguration(c.String("configuration")); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	if err := henchman.InitLog(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	// Step 1: Validate Modules path and see if it exists
	modulesPath := c.String("modules")
	user := c.String("user")
	keyfile := c.String("keyfile")
	_, err := os.Stat(modulesPath)
	if err != nil {
		henchman.Fatal(map[string]interface{}{
			"mod path": modulesPath,
			"error":    err.Error(),
		}, "Error Validating Modules Dir")
	}
	henchman.ModuleSearchPath = append(henchman.ModuleSearchPath, modulesPath)

	// Step 2: Read the inventory
	// FIXME: Support multiple inventory types.
	// We're dealing with YAML for now
	inventoryPath := c.String("inventory")
	inventoryConfig := make(henchman.InventoryConfig)
	inventoryConfig["path"] = inventoryPath
	inventorySource := new(henchman.YAMLInventory)
	tc := make(henchman.TransportConfig)
	tc["username"] = user
	tc["keyfile"] = keyfile

	inv, err := inventorySource.Load(inventoryConfig)
	if err != nil {
		henchErr := henchman.HenchErr(err, map[string]interface{}{
			"error": err.Error(),
		}, "").(*henchman.HenchmanError)
		henchman.Fatal(henchErr.Fields, "Error Loading Inventory")
	}

	// Step 3: Preprocess Plans and execute
	planFile := args[0]
	planBuf, err := ioutil.ReadFile(planFile)
	if err != nil {
		henchErr := henchman.HenchErr(err, map[string]interface{}{
			"plan":  planFile,
			"error": err.Error(),
		}, "").(*henchman.HenchmanError)
		henchman.Fatal(henchErr.Fields, "Error Reading Plan")
	}

	// Step 3.1: Find the groups being used by plan
	groups, err := henchman.GetInventoryGroups(planBuf)
	if err != nil {
		henchErr := henchman.HenchErr(err, map[string]interface{}{
			"plan":  planFile,
			"error": err.Error(),
		}, "").(*henchman.HenchmanError)
		henchman.Fatal(henchErr.Fields, "Error Getting Inv Groups")
	}

	// Step 3.2: Create a filtered Inv with only groups the plan specified
	inventory := inv.GetInventoryForGroups(groups)

	// Step 3.3: For each machine assign group and host vars
	machines, err := inventory.GetMachines(tc)
	if err != nil {
		henchErr := henchman.HenchErr(err, map[string]interface{}{
			"plan":  planFile,
			"error": err.Error(),
		}, "").(*henchman.HenchmanError)
		henchman.Fatal(henchErr.Fields, "Error Getting Machines")
	}
	// Step 3.4: Add entire set of inventory variables in inv
	// to inventory.groups GlobalVars
	inventory.SetGlobalVarsFromInventoryGroups(inv.Groups)

	// Step 3.5: Preprocess plan to create plan struct
	//           Setup final version of vars
	plan, err := henchman.PreprocessPlan(planBuf, &inventory)
	if err != nil {
		henchErr := henchman.HenchErr(err, map[string]interface{}{
			"plan":  planFile,
			"error": err.Error(),
		}, "").(*henchman.HenchmanError)
		henchman.Fatal(henchErr.Fields, "Error Preprocessing Plan")
	}

	// Step 3.5: Setup plan and execute
	if err := plan.Setup(machines); err != nil {
		henchErr := henchman.HenchErr(err, map[string]interface{}{
			"error": err.Error(),
		}, "").(*henchman.HenchmanError)
		henchman.Fatal(henchErr.Fields, "Error in plan setup")
	}

	if err := plan.Execute(machines); err != nil {
		henchErr := henchman.HenchErr(err, map[string]interface{}{
			"error": err.Error(),
		}, "").(*henchman.HenchmanError)
		henchman.Fatal(henchErr.Fields, "Error in executing plan")
	}

	if c.Bool("cleanup") {
		if err := plan.Cleanup(machines); err != nil {
			henchErr := henchman.HenchErr(err, map[string]interface{}{
				"error": err.Error(),
			}, "").(*henchman.HenchmanError)
			henchman.Fatal(henchErr.Fields, "Error in plan cleanup")
		}
	}
}