Exemplo n.º 1
0
func outputLog(out io.Writer) error {
	dvol := api.NewDvolAPI(dvolAPIOptions)
	activeVolume, err := dvol.ActiveVolume()
	if err != nil {
		return err
	}

	activeBranch, err := dvol.ActiveBranch(activeVolume)
	if err != nil {
		return err
	}

	commits, err := dvol.ListCommits(activeVolume, activeBranch)
	if err != nil {
		return err
	}
	for i := len(commits) - 1; i >= 0; i-- {
		commit := commits[i]
		fmt.Fprintf(out, "commit %s\n", commit.Id)
		fmt.Fprint(out, "Author: Who knows <mystery@person>\n")
		fmt.Fprint(out, "Date: Whenever\n\n")
		fmt.Fprintf(out, "    %s\n\n", commit.Message)
	}
	return nil
}
Exemplo n.º 2
0
Arquivo: rm.go Projeto: ClusterHQ/dvol
func removeVolume(cmd *cobra.Command, args []string, out io.Writer) error {
	dvol := api.NewDvolAPI(dvolAPIOptions)
	err := checkVolumeArgs(args)
	if err != nil {
		return err
	}
	volumeName := args[0]
	if !api.ValidName(volumeName) {
		return fmt.Errorf("Error: " + volumeName + " is not a valid name")
	}
	if !dvol.VolumeExists(volumeName) {
		msg := fmt.Sprintf("Volume '%s' does not exist, cannot remove it", volumeName)
		return fmt.Errorf(msg)
	}
	if forceRemoveVolume || userIsSure("This will remove all containers using the volume") {
		s := fmt.Sprintf("Deleting volume '%s'", volumeName)
		fmt.Fprintln(out, s)
		err = dvol.RemoveVolume(volumeName)
		if err != nil {
			return fmt.Errorf("Error removing volume")
		}
	} else {
		fmt.Fprintln(out, "Aborting.")
	}
	return nil
}
Exemplo n.º 3
0
func resetVolume(cmd *cobra.Command, args []string, out io.Writer) error {
	dvol := api.NewDvolAPI(dvolAPIOptions)
	if len(args) != 1 {
		return fmt.Errorf("Must specify one and only one commit identifier.")
	}
	commit := args[0]
	if err := dvol.ResetActiveVolume(commit); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 4
0
func switchVolume(cmd *cobra.Command, args []string, out io.Writer) error {
	dvol := api.NewDvolAPI(dvolAPIOptions)
	err := checkVolumeArgs(args)
	if err != nil {
		return err
	}
	volumeName := args[0]
	if !api.ValidName(volumeName) {
		return fmt.Errorf("Error: " + volumeName + " is not a valid name")
	}
	if !dvol.VolumeExists(volumeName) {
		return fmt.Errorf("Error: " + volumeName + " does not exist")
	}
	err = dvol.SwitchVolume(volumeName)
	if err != nil {
		return fmt.Errorf("Error switching volume")
	}
	return nil
}
Exemplo n.º 5
0
func checkoutBranch(cmd *cobra.Command, args []string, out io.Writer) error {
	dvol := api.NewDvolAPI(dvolAPIOptions)
	if len(args) != 1 {
		return fmt.Errorf("Must specify a branch name.")
	}
	branchName := args[0]
	activeVolume, err := dvol.ActiveVolume()
	if err != nil {
		return err
	}
	activeBranch, err := dvol.ActiveBranch(activeVolume)
	if err != nil {
		return err
	}
	if err := dvol.CheckoutBranch(activeVolume, activeBranch, branchName, createBranch); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 6
0
func commitVolume(cmd *cobra.Command, commitMessage string, out io.Writer) error {
	if commitMessage == "" {
		return fmt.Errorf("You must provide a commit message")
	}
	// TODO need to add user's name and email adddress
	dvol := api.NewDvolAPI(dvolAPIOptions)
	activeVolume, err := dvol.ActiveVolume()
	if err != nil {
		return err
	}
	activeBranch, err := dvol.ActiveBranch(activeVolume)
	if err != nil {
		return err
	}
	commitId, err := dvol.Commit(activeVolume, activeBranch, commitMessage)
	if err != nil {
		return err
	}
	fmt.Printf("%s\n", commitId)
	return nil
}
Exemplo n.º 7
0
func initVolume(cmd *cobra.Command, args []string, out io.Writer) error {
	dvol := api.NewDvolAPI(dvolAPIOptions)
	err := checkVolumeArgs(args)
	if err != nil {
		return err
	}
	volumeName := args[0]
	if !api.ValidName(volumeName) {
		return fmt.Errorf("Error: " + volumeName + " is not a valid name")
	}
	if dvol.VolumeExists(volumeName) {
		return fmt.Errorf("Error: volume " + volumeName + " already exists")
	}
	err = dvol.CreateVolume(volumeName)
	if err != nil {
		return fmt.Errorf("Error creating volume")
	}
	fmt.Fprintln(out, "Created volume", volumeName)
	fmt.Fprintf(out, "Created branch %s/%s\n", volumeName, api.DEFAULT_BRANCH)
	return nil
}
Exemplo n.º 8
0
func volumeDriverList(w http.ResponseWriter, r *http.Request) {
	log.Print("<= /VolumeDriver.List")
	dvol := api.NewDvolAPI(api.DvolAPIOptions{
		BasePath: VOL_DIR,
	})

	allVolumes, err := dvol.AllVolumes()
	if err != nil {
		writeResponseErr(err, w)
	}

	var response = ResponseList{
		Err: "",
	}
	for _, volume := range allVolumes {
		response.Volumes = append(response.Volumes, ResponseListVolume{
			Name:       volume.Name,
			Mountpoint: volume.Path,
		})
	}

	responseJSON, _ := json.Marshal(response)
	w.Write(responseJSON)
}
Exemplo n.º 9
0
func main() {
	log.Print("Starting dvol plugin")

	if _, err := os.Stat(PLUGINS_DIR); err != nil {
		if err := os.MkdirAll(PLUGINS_DIR, 0700); err != nil {
			log.Fatalf("Could not make plugin directory %s: %v", PLUGINS_DIR, err)
		}
	}
	if _, err := os.Stat(DVOL_SOCKET); err == nil {
		if err = os.Remove(DVOL_SOCKET); err != nil {
			log.Fatalf("Could not clean up existing socket at %s: %v", DVOL_SOCKET, err)
		}
	}
	if _, err := os.Stat(VOL_DIR); err != nil {
		if err := os.MkdirAll(VOL_DIR, 0700); err != nil {
			log.Fatalf("Could not make volumes directory %s: %v", VOL_DIR, err)
		}
	}

	http.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
		log.Print("<= /Plugin.Activate")
		responseJSON, _ := json.Marshal(&ResponseImplements{
			Implements: []string{"VolumeDriver"},
		})
		w.Write(responseJSON)
	})

	http.HandleFunc("/VolumeDriver.Create", func(w http.ResponseWriter, r *http.Request) {
		log.Print("<= /VolumeDriver.Create")
		requestJSON, err := ioutil.ReadAll(r.Body)
		if err != nil {
			log.Fatalf("Unable to read response body %s", err)
		}
		request := new(RequestCreate)
		json.Unmarshal(requestJSON, request)
		name := request.Name
		dvol := api.NewDvolAPI(api.DvolAPIOptions{
			BasePath: VOL_DIR,
		})
		if dvol.VolumeExists(name) {
			log.Print("Volume already exists ", name)
		} else {
			err := dvol.CreateVolume(name)
			if err != nil {
				writeResponseErr(fmt.Errorf("Could not create volume %s: %v", name, err), w)
				return
			}
		}
		writeResponseOK(w)
	})

	http.HandleFunc("/VolumeDriver.Remove", func(w http.ResponseWriter, r *http.Request) {
		/*
			We do not actually want to remove the dvol volume when Docker references to them are removed.

			This is a no-op.
		*/
		writeResponseOK(w)
	})

	http.HandleFunc("/VolumeDriver.Path", func(w http.ResponseWriter, r *http.Request) {
	})

	http.HandleFunc("/VolumeDriver.Mount", func(w http.ResponseWriter, r *http.Request) {
		log.Print("<= /VolumeDriver.Mount")
		requestJSON, err := ioutil.ReadAll(r.Body)
		if err != nil {
			log.Fatalf("Unable to read response body %s", err)
		}
		request := new(RequestMount)
		json.Unmarshal(requestJSON, request)
		name := request.Name

		dvol := api.NewDvolAPI(api.DvolAPIOptions{
			BasePath: VOL_DIR,
		})

		if !dvol.VolumeExists(name) {
			// Volume does not exist. We will implicitly create it.
			log.Printf("Attempting to mount unknown volume %s, creating it", name)
			err := dvol.CreateVolume(name)
			if err != nil {
				writeResponseErr(fmt.Errorf("Could not create volume %s: %v", name, err), w)
				return
			}
		}

		err = dvol.SwitchVolume(name)
		if err != nil {
			writeResponseErr(err, w)
			return
		}
		_, err = dvol.ActiveVolume()
		if err != nil {
			writeResponseErr(err, w)
			return
		}
		path := dvol.VolumePath(name)

		responseJSON, _ := json.Marshal(&ResponseMount{
			Mountpoint: path,
			Err:        "",
		})
		w.Write(responseJSON)
	})

	http.HandleFunc("/VolumeDriver.Unmount", func(w http.ResponseWriter, r *http.Request) {
		writeResponseOK(w)
	})

	http.HandleFunc("/VolumeDriver.List", volumeDriverList)

	listener, err := net.Listen("unix", DVOL_SOCKET)
	if err != nil {
		log.Fatalf("Could not listen on %s: %v", DVOL_SOCKET, err)
	}

	http.Serve(listener, nil)
}