-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c24b8d
commit 89535e0
Showing
15 changed files
with
577 additions
and
61 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<component type="addon"> | ||
<id>org.cockpit_project.subscriptions</id> | ||
<metadata_license>CC0-1.0</metadata_license> | ||
<name>Subscriptions</name> | ||
<summary>Scaffolding for a cockpit module</summary> | ||
<description> | ||
<p> | ||
Scaffolding for a cockpit module. | ||
</p> | ||
</description> | ||
<extends>org.cockpit_project.subscriptions</extends> | ||
<launchable type="cockpit-manifest">subscriptions</launchable> | ||
<url type="homepage">https://github.com/cockpit-project/subscriptions</url> | ||
<url type="bugtracker">https://github.com/cockpit-project/subscriptions/issues</url> | ||
<update_contact>cockpit-devel_AT_lists.fedorahosted.org</update_contact> | ||
</component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
de |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
@use "@patternfly/patternfly/patternfly-addons"; | ||
@use "page.scss"; | ||
|
||
p { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,144 @@ | ||
/* | ||
* This file is part of Cockpit. | ||
* | ||
* Copyright (C) 2017 Red Hat, Inc. | ||
* | ||
* Cockpit is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation; either version 2.1 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Cockpit is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react'; | ||
import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import { Card, CardBody, CardTitle } from "@patternfly/react-core/dist/esm/components/Card/index.js"; | ||
|
||
import cockpit from 'cockpit'; | ||
|
||
const _ = cockpit.gettext; | ||
import { Page, PageSection, PageSectionVariants } from '@patternfly/react-core'; | ||
import { Backend, Extension, Subscription } from './backends/backend'; | ||
import { TransactionalUpdate } from './backends/transactional-update'; | ||
import { SuseConnect } from './backends/suseconnect'; | ||
import { RegisterCodeForm, RegisterFormData } from './components/register_code_form'; | ||
import { SubscriptionList } from './components/subscription_list'; | ||
import { useDialogs } from 'dialogs'; | ||
import { RebootDialog } from './components/reboot_dialog'; | ||
|
||
export const Application = () => { | ||
const [hostname, setHostname] = useState(_("Unknown")); | ||
const [backend, setBackend] = useState<Backend | null>(null); | ||
const [loadingSubscriptions, setLoadingSubscriptions] = useState<boolean>(true); | ||
const [loadingExtensions, setLoadingExtensions] = useState<boolean>(true); | ||
const [subscriptions, setSubscriptions] = useState<Subscription[]>([]); | ||
const [unregisteredSubscriptions, setUnregisteredSubscriptions] = useState<Extension[]>([]); | ||
const [formData, setFormData] = useState<RegisterFormData>({ | ||
registrationCode: "", | ||
email: "", | ||
product: "", | ||
}); | ||
|
||
const Dialogs = useDialogs(); | ||
|
||
useEffect(() => { | ||
cockpit.spawn(["test", "-e", "/sbin/transactional-update"], { err: "ignore" }) | ||
.then(() => setBackend(new TransactionalUpdate())) | ||
.catch(() => { | ||
cockpit.spawn(["test", "-e", "/usr/bin/suseconnect"], { err: "ignore" }) | ||
.then(() => setBackend(new SuseConnect())); | ||
}); | ||
}, [setBackend]); | ||
|
||
const updateSubscriptions = useCallback(() => { | ||
if (backend === null) { | ||
return; | ||
} | ||
setLoadingSubscriptions(true); | ||
|
||
// handle get subscriptions | ||
backend.getSubscriptions() | ||
.then((subscriptions) => { | ||
setSubscriptions(subscriptions); | ||
setLoadingSubscriptions(false); | ||
}); | ||
|
||
backend.getExtensions() | ||
.then((subscriptions) => { | ||
setUnregisteredSubscriptions(subscriptions); | ||
setLoadingExtensions(false); | ||
}); | ||
}, [backend, setLoadingSubscriptions, setSubscriptions, setUnregisteredSubscriptions, setLoadingExtensions]); | ||
|
||
useEffect(() => { | ||
const hostname = cockpit.file('/etc/hostname'); | ||
hostname.watch(content => setHostname(content?.trim() ?? "")); | ||
return hostname.close; | ||
}, []); | ||
updateSubscriptions(); | ||
}, [updateSubscriptions, backend]); | ||
|
||
const registerProduct = async (): Promise<[boolean, string]> => { | ||
console.debug("registering", formData); | ||
const result = await backend?.register(formData.registrationCode, formData.email, formData.product).then((result) => { | ||
if (result[0]) { | ||
if (result[1].includes("Please reboot your machine")) { | ||
// Show reboot modal | ||
Dialogs.show(<RebootDialog />); | ||
return result; | ||
} | ||
|
||
updateSubscriptions(); | ||
return result; | ||
} | ||
|
||
return result; | ||
}); | ||
|
||
return result || [false, ""]; | ||
}; | ||
|
||
const deactivateProduct = (subscription: Subscription | Extension): void => { | ||
console.log("deregistering", subscription.identifier); | ||
setLoadingSubscriptions(true); | ||
backend?.deregister([subscription.identifier, subscription.version, subscription.arch].join("/")) | ||
.then(async (output) => { | ||
console.log(output); | ||
if (output.includes("Can not deregister base product")) { | ||
console.log("deregistering base"); | ||
output = await backend.deregister(); | ||
console.log("base deregistered"); | ||
} | ||
if (output.includes("Please reboot your machine")) { | ||
Dialogs.show(<RebootDialog />); | ||
} | ||
console.log("deregistered"); | ||
setLoadingSubscriptions(false); | ||
updateSubscriptions(); | ||
}); | ||
}; | ||
|
||
const activateProduct = (subscription: Subscription | Extension): void => { | ||
console.log("activating", subscription.identifier); | ||
setLoadingExtensions(true); | ||
backend?.register("", "", [subscription.identifier, subscription.version, subscription.arch].join("/")) | ||
.then(async (result) => { | ||
if (result[0]) { | ||
if (result[1].includes("Please reboot your machine")) { | ||
// Show reboot modal | ||
Dialogs.show(<RebootDialog />); | ||
} | ||
} | ||
console.log("activated"); | ||
setLoadingExtensions(false); | ||
updateSubscriptions(); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Card> | ||
<CardTitle>Starter Kit</CardTitle> | ||
<CardBody> | ||
<Alert | ||
variant="info" | ||
title={ cockpit.format(_("Running on $0"), hostname) } | ||
/> | ||
</CardBody> | ||
</Card> | ||
<Page> | ||
<PageSection variant={PageSectionVariants.light}> | ||
<Card> | ||
<CardTitle>Register a new subscription</CardTitle> | ||
<CardBody> | ||
<RegisterCodeForm submitCallback={registerProduct} formData={formData} setFormData={setFormData} /> | ||
</CardBody> | ||
</Card> | ||
<Card> | ||
<CardTitle>Registered Subscriptions</CardTitle> | ||
<CardBody> | ||
<SubscriptionList subscriptions={subscriptions} loading={loadingSubscriptions} deactivate={deactivateProduct} /> | ||
</CardBody> | ||
</Card> | ||
{unregisteredSubscriptions.length | ||
? <Card> | ||
<CardTitle>Available Extensions</CardTitle> | ||
<CardBody> | ||
<SubscriptionList subscriptions={unregisteredSubscriptions} loading={loadingExtensions} activate={activateProduct} /> | ||
</CardBody> | ||
</Card> | ||
: ""} | ||
</PageSection> | ||
</Page> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
type Extension = { | ||
name: string, | ||
identifier: string, | ||
version: string, | ||
arch: string, | ||
activated: boolean, | ||
available: boolean, | ||
free: boolean | ||
extensions: Extension[], | ||
expires_at?: string, | ||
} | ||
|
||
enum SubscriptionStatus { | ||
Active = "Active", | ||
Expired = "Expired", | ||
Unregistered = "Unregistered", | ||
} | ||
|
||
type Subscription = { | ||
name?: string, | ||
identifier: string, | ||
version: string, | ||
arch: string, | ||
status: string, | ||
regcode?: string, | ||
starts_at?: string, | ||
expires_at?: string, | ||
subscription_status?: SubscriptionStatus, | ||
type?: string, | ||
extensions?: Extension[], | ||
} | ||
|
||
// Cockpits typescript types don't contain this | ||
// Just a more slimmed down BasicError, might be worth upstreaming | ||
type CockpitSpawnError = { | ||
message: string, | ||
problem: string | null, | ||
exit_status: number | null, | ||
exit_signal: string | null, | ||
} | ||
|
||
enum SUSEConnectExitCodes { | ||
ZyppBusy = 7, | ||
} | ||
|
||
interface Backend { | ||
getSubscriptions(): Promise<Subscription[]>; | ||
|
||
getExtensions(): Promise<Extension[]>; | ||
|
||
register(reg_code?: string, email?: string, product?: string): Promise<[boolean, string]>; | ||
|
||
deregister(product?: string): Promise<string>; | ||
} | ||
|
||
export { Backend, Subscription, SubscriptionStatus, Extension, CockpitSpawnError, SUSEConnectExitCodes }; |
Oops, something went wrong.