summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageOvfConversionPage.java
blob: 8caa4c510514409061b626cf38e6daf330ced550 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.openslx.dozmod.gui.wizard.page;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.openslx.dozmod.Config;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.I18n;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.QFileChooser;
import org.openslx.dozmod.gui.wizard.Wizard;
import org.openslx.dozmod.gui.wizard.layout.ImageOvfConversionPageLayout;
import org.openslx.dozmod.state.UploadWizardState;
import org.openslx.dozmod.util.ConversionTaskWorker;

/**
 * Page for converting an ovf image into an vmx image. Creates a directory for
 * the new vmx image, starts the conversion SwingWorker and shows the progress.
 * Replaces the image description file in state after conversion.
 */
public class ImageOvfConversionPage extends ImageOvfConversionPageLayout {
	/**
	 * Version for serialization.
	 */
	private static final long serialVersionUID = 464428060782110490L;

	private final static Logger LOGGER = Logger.getLogger(ImageOvfConversionPage.class);

    private UploadWizardState state;
    private ImageOvfConversionPage page;
    private File vmxFile;
    private ConversionTaskWorker worker;
    private String startConversionButtonString = I18n.PAGE
            .getString("ImageOvfConversion.StartConversionButton.text");
    public boolean conversionStarted = false;
    public boolean conversionSuccessful = false;
    private String ovfToolPath = "ovftool";

    public ImageOvfConversionPage(Wizard wizard, UploadWizardState uploadWizardState) {
        super(wizard);
        setPageComplete(false);
        this.canComeBack = false;
        this.state = uploadWizardState;
        page = this;
        String os = System.getProperty("os.name");
        // Linux install should have put the ovftool into the path variable.
        // Try to set it for windows and macos.
        if (os.toLowerCase().contains("windows")) {
            ovfToolPath = System.getenv("ProgramFiles(X86)")
                    + "\\VMware\\VMware Workstation\\OVFTool\\ovftool.exe";
            if (!(new File(ovfToolPath).exists())) {
                ovfToolPath = System.getenv("ProgramFiles(X86)")
                        + "\\VMware\\VMware Player\\OVFTool\\ovftool.exe";
            }
        } else if (os.toLowerCase().contains("mac")) {
            ovfToolPath = "/Applications/VMware Fusion.app/Contents/Library/VMware OVF Tool";
        }

        txtInfoText.setVisible(true);

        // Start the conversion
        btnStartConversion.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (worker == null) {
                    try {
                        updateConversionProgressbar(0);
                        btnStartConversion.setText(
                                I18n.PAGE.getString("ImageOvfConversion.AbortConversionButton.text"));
                        convertOvfToVmx(state.descriptionFile);
                    } catch (Exception error) {
                        btnStartConversion.setEnabled(false);
                        Gui.showMessageBox(page,
                                I18n.PAGE.getString("ImageOvfConversion.ErrorMessage.ConversionFailed"),
                                MessageType.ERROR, LOGGER, error);
                    }
                } else {
                    page.cancelConversionWorker();

                }
            }
        });
        // Browse for *.vmx
        btnBrowseForOvftool.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                browseForOvftoolPath();
            }

        });
    }

    // Check wether the file contains the keyword. Stops after the first few lines.
    private boolean fileContainsKeyword(File file, String keyword) {
        try (Reader reader = new FileReader(file); BufferedReader buffered = new BufferedReader(reader)) {
            String line;
            int lineCount = 0;
            while ((line = buffered.readLine()) != null && lineCount < 50) {
                if (line.toLowerCase().contains(keyword)) {
                    LOGGER.debug("Detected OVF/OVA created with Virtual Box");
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void browseForOvftoolPath() {
        QFileChooser fc = new QFileChooser(Config.getUploadPath(), false);
        int action = fc.showOpenDialog(getDialog());
        File file = fc.getSelectedFile();

        if (action != JFileChooser.APPROVE_OPTION || file == null) {
            return;
        }
        page.ovfToolPath = file.getAbsolutePath();
        btnStartConversion.setEnabled(true);
        ovfToolFile.setText(page.ovfToolPath);
    }

    public void cancelConversionWorker() {
        if (worker != null) {
            worker.cancel(true);
            btnStartConversion.setText(startConversionButtonString);
            worker = null;
        }
    }

    // Check if the directory we want to create for the converted virtual machine
    // exists and create it
    private void convertOvfToVmx(File file) throws IOException {
        File directoryFile = new File(file.getParent() + "/converted_vm");
        LOGGER.debug("Directory for converted VM:" + directoryFile.getAbsolutePath());
        // Cancel if user clicks cancel
        if (directoryFile.exists() && !askDeleteDirAndContinue(directoryFile)) {
            btnStartConversion.setText(startConversionButtonString);
            return;
        }
        conversionStarted = true;
        Files.createDirectories(directoryFile.toPath());
        vmxFile = new File(
                directoryFile.getPath() + "/" + FilenameUtils.removeExtension((file.getName())) + ".vmx");
        worker = new ConversionTaskWorker(file, vmxFile, page, ovfToolPath);
        worker.execute();
        state.convertedDescriptionFile = vmxFile;
    }

    private boolean askDeleteDirAndContinue(File directoryFile) throws IOException {
        if (directoryFile.exists()) {
            int dialogButton = JOptionPane.YES_NO_OPTION;
            int dialogResult;
            if (!conversionStarted) {
                dialogResult = JOptionPane.showConfirmDialog(this,
                        I18n.PAGE.getString("ImageOvfConversion.Dialog.DirectoryExists"),
                        I18n.PAGE.getString("ImageOvfConversion.Dialog.DirectoryExists.title"), dialogButton);
            } else {
                dialogResult = JOptionPane.showConfirmDialog(this,
                        I18n.PAGE.getString("ImageOvfConversion.Dialog.RemoveTmpDirectory"),
                        I18n.PAGE.getString("ImageOvfConversion.Dialog.RemoveTmpDirectory.title"),
                        dialogButton);
            }
            if (dialogResult == 0) {
                FileUtils.deleteDirectory(directoryFile);
                return true;
            } else {
                return false;
            }
        }
        // Nothing to delete. Should be OK to continue
        return true;
    }

    public void updateConversionProgressbar(int percent) {
        conversionProgressBar.setValue(percent);
    }

    public void updateConversionProgressbarText(String text) {
        conversionProgressBar.setString(text);
    }

    @Override
    protected boolean wantNextOrFinish() {
        return false;
    }

    @Override
    protected void onPageEnter() {
        if (!fileContainsKeyword(state.descriptionFile, "vmware")) {
            Gui.showMessageBox(this,
                    I18n.PAGE.getString("ImageOvfConversion.MessageBox.notAVmwareOrVboxImage"),
                    MessageType.WARNING, LOGGER, null);
        } else if (fileContainsKeyword(state.descriptionFile, "vbox")) {
            Gui.showMessageBox(this, I18n.PAGE.getString("ImageOvfConversion.MessageBox.VboxDetected"),
                    MessageType.INFO, LOGGER, null);
        }
        updateConversionProgressbar(0);
        updateConversionProgressbarText("");
        conversionStarted = false;
        conversionSuccessful = false;
        btnStartConversion.setText(startConversionButtonString);
    }

    @Override
    protected void onPageLeave() {
        // Make sure the worker is stopped when leaving the page.
        if (wizard.isCancelled() || (conversionStarted && !conversionSuccessful)) {
            page.cancelConversionWorker();
            if (state.convertedDescriptionFile != null) {
                try {
                    askDeleteDirAndContinue(state.convertedDescriptionFile.getParentFile());
                } catch (IOException e) {
                    LOGGER.debug(e);
                }
            }
            state.convertedDescriptionFile = null;
            conversionStarted = false;
        }
        super.onPageLeave();
    }

}